Object

Write a program to calculate the roots of a quadratic equation.
Hint: Quadratic Formula: If ax2 + bx + c = 0, then

Algorithm

  1. Declare integer variables a, b and c and double variables x1, x2, x3, d, x, r and i.
  2. DO
    • Read a, b and c.
    • Set d = (b^2)-(4*a*c).
    • IF d = 0, then
      • Set x = -b/(2*a).
      • Write x.
    • ELSE IF d < 0, then
      • Set i = ((-d^0.5)/(2*a).
      • Set r = -b/(2*a).
      • Write r and i.
    • ELSE
      • Set x1 = (-b + d^0.5))/(2*a).
      • Set x2 = (-b – d^0.5))/(2*a).
      • Write x1 and x2.
    • [End of IF-ELSE structure.]
    • Read character.
    • WHILE character = ‘y’.
    • [End of DO-WHILE loop.]
  3. Exit.

Flowchart

C++ Source Code

// program 70
#include<iostream>
#include <cmath>
using namespace std;

int main()
{
  int a, b, c;
  double x1, x2, x3, d, x, r, i;
  char choice;

  do
  {
    cout << "\nEnter the coefficient of 'x^2': ";
    cin >> a;
    cout << "Enter the coefficient of 'x': ";
    cin >> b;
    cout << "Enter the constant: ";
    cin >> c;

    cout << "\nThe equation is " << a << "x^2 + " << b << "x + " << c << " = 0";
    d = pow(b, 2) - (4 * a * c);

    if (d == 0)
    {
      x = -b / (2.0 * a);
      cout << "\nThere is a single root: x = " << x;
    }
    else if (d < 0)
    {
      cout << "\nThe roots are imaginary: ";
      i = sqrt(-d) / (2.0 * a);
      r = -b / (2.0 * a);
      cout << "\nReal part is = " << r;
      cout << "\nImaginary part is = " << i;
    }
    else
    {
      x1 = (-b + sqrt(d)) / (2.0 * a);
      x2 = (-b - sqrt(d)) / (2.0 * a);
      cout << "\nThe roots are x1 = " << x1 << " and x2 = " << x2;
    }

    cout << "\n\nDo you want to continue (y or n): ";
    cin >> choice;
  } 
  while (choice == 'y' || choice == 'Y');

  return 0;
}

C Source Code

/*program 70*/
#include <stdio.h>
#include <math.h>

int main() {
  int a, b, c;
  double x1, x2, x3, d, x, r, i;
  char choice;

  do 
  {
    printf("\nEnter the coefficient of 'x^2': ");
    scanf("%d", &a);
    printf("Enter the coefficient of 'x': ");
    scanf("%d", &b);
    printf("Enter the constant: ");
    scanf("%d", &c);

    printf("\nThe equation is %dx^2 + %dx + %d = 0", a, b, c);
    d = pow(b, 2) - (4 * a * c);

    if (d == 0) 
    {
      x = -b / (2.0 * a);
      printf("\nThere is a single root: x = %0.2f", x);
    } 
    else if (d < 0) 
    {
      printf("\nThe roots are imaginary: ");
      i = pow(-d,0.5) / (2.0 * a);
      r = -b / (2.0 * a);
      printf("\nReal part is = %0.2f", r);
      printf("\nImaginary part is = %0.2f", i);
    } 
    else 
    {
      x1 = (-b + pow(d,0.5)) / (2.0 * a);
      x2 = (-b - pow(d,0.5)) / (2.0 * a);
      printf("\nThe roots are x1 = %0.2f and x2 = %0.2f", x1, x2);
    }

    printf("\n\nDo you want to continue (y or n): ");
    scanf(" %c", &choice);
  } 
  while (choice == 'y' || choice == 'Y');

  return 0;
}

Output

Enter the coefficient of ‘x^2’: 2
Enter the coefficient of ‘x’: 8
Enter the constant: 10

The equation is 2x^2 + 8x + 10 = 0
The roots are imaginary:
Real part is = -2
Imaginary part is = 1

Do you want to continue (y or n):

Design a site like this with WordPress.com
Get started