Saturday, November 3, 2018

25. WAP to find the roots of a quadratic equation.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
   float a,b,c,disc,root1,root2,img;
   printf("Enter the values of a, b and c for the Quadratic Equation "
      "ax^2 + bx + c:\n");
   printf("a: ");
   scanf("%f",&a);
   printf("b: ");
   scanf("%f",&b);
   printf("c: ");
   scanf("%f",&c);
   disc = b*b - 4*a*c;
   if(disc>0)
   {
root1 = (-b + pow(disc,0.5)) / (2*a);
root2 = (-b - pow(disc,0.5)) / (2*a);
        printf("\nTwo distinct and real roots exists: %0.2f, %0.2f",root1,root2);
   }
   else if(disc==0)
   {
    root1 = -b / (2*a);
        root2 = root1;  
        printf("\nTwo equal and real roots exists: %0.2f, %0.2f",root1,root2);
   }
   else
   {
    root1 = -b / (2*a);
        root2 = root1;
        img = pow(-disc,0.5) / (2*a);
        printf("\nTwo distinct and complex roots exists: %0.2f + i%0.2f, "
      "%0.2f - i%0.2f",root1,img,root2,img);
   }
   getch();
}


Output:




Please have a look at the below lesson posted on my YouTube channel:


No comments:

Post a Comment