Monday, June 18, 2012

To find whether the roots of quadratic equation are real or imaginary


/* To find whether the roots of quadratic equation are real or imaginary */

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

 int a,b,c,d;

 float x1,x2;

 clrscr();

 printf("For a given quadratic equation\n...enter the value of a,b,c:\n");

 scanf("%d%d%d",&a,&b,&c);

 /* To calculate determinant of quadratic equation */

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

 /* To check whether the roots are real or imaginary */

 if(d>=0)

 {

   printf("\nRoots are real!");

   x1=(-(b) + sqrt(d))/(2*a);

   x2=(-(b) - sqrt(d))/(2*a);

   printf("\nRoots are %f and %f",x1,x2);

 }

 else

   printf("\nRoots are imaginary!");

 getch();

}

 

OUTPUT

For a given quadratic equation

...enter the value of a,b,c:

1

3

2

 

Roots are real!

Roots are -1.000000 and -2.000000

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.