0
8.7kviews
C program to find roots of a quadratic equation
1 Answer
1
1.8kviews

C program to find roots of a quadratic equation

C program to find roots of a quadratic equation: This program calculates roots of a quadratic equation. Coefficients are assumed to be integers, but roots may not be real. Discriminant (bb-4a*c) decides the nature of roots. In the program, j stands for iota. C programming code

#include <stdio.h>
#include <math.h>

int main()
{
   int a, b, c, d;
   double root1, root2;

   printf("Enter a, b and c where a*x*x + b*x + c = 0\n");
   scanf("%d%d%d", &a, &b, &c);

   d = b*b - 4*a*c;

   if (d < 0) { //complex roots
     printf("First root = %.2lf + j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
     printf("Second root = %.2lf - j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
   }
   else { //real roots
      root1 = (-b + sqrt(d))/(2*a);
      root2 = (-b - sqrt(d))/(2*a);

      printf("First root = %.2lf\n", root1);
      printf("Second root = %.2lf\n", root2);
   }

   return 0;
}
Please log in to add an answer.