StudyLover
  • Home
  • Study Zone
  • Profiles
  • Contact us
  • Sign in
StudyLover 5. Program to find the roots of a quadratic equation.
Download
  1. C++ Programs list
4. Program to find the greatest common divisor (GCD). : 6. Program to check whether a given number is positive or negative using the ternary operator.
C++ Programs list

1.      Program to find the roots of a quadratic equation.

  1. #include <iostream.h>

  2. #include <math.h>

  3. #include <conio.h>

  4. int main() {

  5.     float a, b, c, discriminant, root1, root2, realPart, imagPart;

  6.     cout << "Enter coefficients a, b, and c: ";

  7.     cin >> a >> b >> c;

  8.     discriminant = b * b - 4 * a * c;

  9.     if (discriminant > 0) {

  10.         root1 = (-b + sqrt(discriminant)) / (2 * a);

  11.         root2 = (-b - sqrt(discriminant)) / (2 * a);

  12.         cout << "Roots are real and different." << endl;

  13.         cout << "Root 1 = " << root1 << endl;

  14.         cout << "Root 2 = " << root2 << endl;

  15.     } else if (discriminant == 0) {

  16.         root1 = root2 = -b / (2 * a);

  17.                 cout << "Roots are real and same." << endl;

  18.         cout << "Root 1 = Root 2 = " << root1 << endl;

  19.     } else {

  20.                 realPart = -b / (2 * a);

  21.                 imagPart = sqrt(-discriminant) / (2 * a);

  22.                 cout << "Roots are complex and different." << endl;

  23.                 cout << "Root 1 = " << realPart << " + i" << imagPart << endl;

  24.                 cout << "Root 2 = " << realPart << " - i" << imagPart << endl;

  25.     }

  26.     getch();

  27.     return 0;

  28. }

     

  29. 1.      Program to find the roots of a quadratic equation.

  30. #include <iostream.h>

  31. #include <math.h>

  32. #include <conio.h>

  33. int main() {

  34.     float a, b, c, discriminant, root1, root2, realPart, imagPart;

  35.     cout << "Enter coefficients a, b, and c: ";

  36.     cin >> a >> b >> c;

  37.     discriminant = b * b - 4 * a * c;

  38.     if (discriminant > 0) {

  39.         root1 = (-b + sqrt(discriminant)) / (2 * a);

  40.         root2 = (-b - sqrt(discriminant)) / (2 * a);

  41.         cout << "Roots are real and different." << endl;

  42.         cout << "Root 1 = " << root1 << endl;

  43.         cout << "Root 2 = " << root2 << endl;

  44.     } else if (discriminant == 0) {

  45.         root1 = root2 = -b / (2 * a);

  46.                 cout << "Roots are real and same." << endl;

  47.         cout << "Root 1 = Root 2 = " << root1 << endl;

  48.     } else {

  49.                 realPart = -b / (2 * a);

  50.                 imagPart = sqrt(-discriminant) / (2 * a);

  51.                 cout << "Roots are complex and different." << endl;

  52.                 cout << "Root 1 = " << realPart << " + i" << imagPart << endl;

  53.                 cout << "Root 2 = " << realPart << " - i" << imagPart << endl;

  54.     }

  55.     getch();

  56.     return 0;

  57. }

4. Program to find the greatest common divisor (GCD). 6. Program to check whether a given number is positive or negative using the ternary operator.
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website