Assignment Document

Roots of a Quadratic Equation

Pages:

Preview:


  • "1) Find All Roots of a Quadratic Equation2 For a quadratic equation ax +bx+c = 0 (where a, b and c are coefficients), it'sroots is given by following the formula.- b± v( b2-4ac)/2a2 The term b -4ac is known as the determinant of a quadratic..

Preview Container:


  • "1) Find All Roots of a Quadratic Equation2 For a quadratic equation ax +bx+c = 0 (where a, b and c are coefficients), it'sroots is given by following the formula.- b± v( b2-4ac)/2a2 The term b -4ac is known as the determinant of a quadratic equation. Thedeterminant tells the nature of the roots.? If determinant is greater than 0, the roots are real and different.? If determinant is equal to 0, the roots are real and equal.? If determinant is less than 0, the roots are complex and different.#include <iostream>#include <cmath>using namespace std;int main() {float a, b, c, x1, x2, determinant, realPart, imaginaryPart;cout << "Enter coefficients a, b and c: ";cin >> a >> b >> c;determinant = b*b - 4*a*c;if (determinant > 0) { x1 = (-b + sqrt(determinant)) / (2*a);x2 = (-b - sqrt(determinant)) / (2*a);cout << "Roots are real and different." << endl;cout << "x1 = " << x1 << endl;cout << "x2 = " << x2 << endl;}else if (determinant == 0) {cout << "Roots are real and same." << endl;x1 = (-b + sqrt(determinant)) / (2*a);cout << "x1 = x2 =" << x1 << endl;}else {realPart = -b/(2*a);imaginaryPart =sqrt(-determinant)/(2*a);cout << "Roots are complex and different."<< endl;cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;}return 0;} 1) Find Factorial of a given numberFor any positive number n, it's factorial is given by:factorial = 1*2*3...*nFactorial of negative number cannot be found and factorial of 0 is 1.In this program below, user is asked to enter a positive integer. Then the factorialof that number is computed and displayed in the screen.#include <iostream>using namespace std;int main(){unsigned int n;unsigned long long factorial = 1;cout << "Enter a positive integer: ";cin >> n;for(int i = 1; i <=n; ++i){factorial *= i; }cout << "Factorial of " << n << " = " << factorial;return 0;}2) Display Fibonacci SeriesThe Fibonacci sequence is a series where the next term is the sum of pervioustwo terms. The first two terms of the Fibonacci sequence is 0 followed by 1.The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21#include <iostream>using namespace std;int main(){int n, t1 = 0, t2 = 1, nextTerm = 0;cout << "Enter the number of terms: ";cin >> n;cout << "Fibonacci Series: ";for (int i = 1; i <= n; ++i) {// Prints the first two terms.if(i == 1){cout << " " << t1;continue;}if(i == 2){cout << t2 << " ";continue;}nextTerm = t1 + t2;t1 = t2;t2 = nextTerm;cout << nextTerm << " ";}return 0;}3) Check Whether a Number is Palindrome or NotThis program takes an integer from user and that integer is reversed. If the reversed integer is equal to the integer entered by user then, that number isa palindrome if not that number is not a palindrome.#include <iostream>using namespace std;int main(){ int n, num, digit, rev = 0; cout << "Enter a positive number: "; cin >> num; n = num; do { digit = num % 10; rev = (rev * 10) + digit; num = num / 10; } while (num != 0); cout << " The reverse of the number is: " << rev << endl; if (n == rev)cout << " The number is a palindrome"; else cout << " The number is not a palindrome";return 0;}4) Check Whether a Number is Prime or NotA positive integer which is only divisible by 1 and itself is known as primenumber.For example: 13 is a prime number because it is only divisible by 1 and 13 but,15 is not prime number because it is divisible by 1, 3, 5 and 15.#include <iostream>using namespace std;int main(){int n, i;bool isPrime = true;cout << "Enter a positive integer: ";cin >> n; for(i = 2; i <= n / 2; ++i){if(n % i == 0){isPrime = false;break;}}if (isPrime)cout << "This is a prime number";elsecout << "This is not a prime number";return 0;}5) Make a Simple Calculator to Add, Subtract, Multiply or DivideUsing switch...caseThis program takes an arithmetic operator (+, -, *, /) and two operands from anuser and performs the operation on those two operands depending upon theoperator entered by user.# include <iostream>using namespace std;int main() {char op;float num1, num2;cout << "Enter operator either + or - or * or /: ";cin >> op;cout << "Enter two operands: ";cin >> num1 >> num2;switch(op){case '+':cout << num1+num2;break;case '-':cout << num1-num2;break;case '*':cout << num1*num2;break;case '/': cout << num1/num2;break;default:// If the operator is other than +, -, * or /, error message is showncout << "Error! operator is not correct";break;}return 0;}6) Calculate Factorial of a Number Using RecursionThis program takes a positive integer from user and calculates the factorial ofthat number. Suppose, user enters 6 then,Factorial will be equal to 1*2*3*4*5*6 = 720You'll learn to find the factorial of a number using a recursive function in thisexample.#include<iostream>using namespace std;int factorial(int n); int main(){int n;cout << "Enter a positive integer: ";cin >> n;cout << "Factorial of " << n << " = " << factorial(n);return 0;}int factorial(int n){if(n > 1)return n * factorial(n - 1);elsereturn 1;}7) Find Largest Element of an ArrayThis program takes n number of element from user(where, n is specified by user)and stores data in an array. Then, this program displays the largest element ofthat array using loops.#include <iostream> using namespace std;int main(){int i, n;float arr[100];cout << "Enter total number of elements(1 to 100): ";cin >> n;cout << endl;// Store number entered by the userfor(i = 0; i < n; ++i){ cout << "Enter Number " << i + 1 << " : "; cin >> arr[i];}// Loop to store largest number to arr[0]for(i = 1;i < n; ++i){ // Change < to > if you want to find the smallest element if(arr[0] < arr[i]) arr[0] = arr[i];} cout << "Largest element = " << arr[0];return 0;}8) Add Two Matrix Using Multi-dimensional ArraysIn this program, user is asked to entered the number of rows r and columns c.The value of r and c should be less than 100 in this program.The user is asked to enter elements of two matrices (of order r*c).Then, the program adds these two matrices, saves it in another matrix (two- dimensional array) and displays it on the screen.#include <iostream>using namespace std;int main(){int r, c, a[100][100], b[100][100], sum[100][100], i, j;cout << "Enter number of rows (between 1 and 100): ";cin >> r;cout << "Enter number of columns (between 1 and 100): ";cin >> c; cout << endl << "Enter elements of 1st matrix: " << endl;// Storing elements of first matrix entered by user.for(i = 0; i < r; ++i) for(j = 0; j < c; ++j) { cout << "Enter element a" << i + 1 << j + 1 << " : "; cin >> a[i][j]; }// Storing elements of second matrix entered by user.cout << endl << "Enter elements of 2nd matrix: " << endl;for(i = 0; i < r; ++i) for(j = 0; j < c; ++j) { cout << "Enter element b" << i + 1 << j + 1 << " : "; cin >> b[i][j]; }// Adding Two matricesfor(i = 0; i < r; ++i)for(j = 0; j < c; ++j)sum[i][j] = a[i][j] + b[i][j];// Displaying the resultant sum matrix. cout << endl << "Sum of two matrix is: " << endl;for(i = 0; i < r; ++i)for(j = 0; j < c; ++j){cout << sum[i][j] << "";if(j == c - 1)cout << endl;}return 0;}9) Multiply Two Matrix Using Multi-dimensional ArraysTo multiply two matrices, the number of columns of first matrix should be equal tothe number of rows to second matrix. This program displays the error until thenumber of columns of first matrix is equal to the number of rows of secondmatrix.#include <iostream>using namespace std;int main(){int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;cout << "Enter rows and columns for first matrix: "; "

Related Documents

Start searching more documents, lectures and notes - A complete study guide!
More than 25,19,89,788+ documents are uploaded!

Why US?

Because we aim to spread high-quality education or digital products, thus our services are used worldwide.
Few Reasons to Build Trust with Students.

128+

Countries

24x7

Hours of Working

89.2 %

Customer Retention

9521+

Experts Team

7+

Years of Business

9,67,789 +

Solved Problems

Search Solved Classroom Assignments & Textbook Solutions

A huge collection of quality study resources. More than 18,98,789 solved problems, classroom assignments, textbooks solutions.

Scroll to Top