Thursday 23 May 2013

FIND THE ROOTS OF A QUADRATIC EQUATION

The following program finds the roots of a quadratic equation. The salient features of this program are mentioned below :-
  1. It tells you whether the roots are real or complex.
  2. This program even gives the complex roots.
  3. It shows whether the roots are equal or unequal.
Formula used :-


Source code:-
/****************************************************************************/
#include<stdio.h>
#include<conio.h>

int main()
{
float a,b,c,d,root1,root2,x,y;
clrscr();
printf("THIS PROGRAM CALCULATES THE ROOTS OF A QUADRATIC EQUATION\n");
printf("ENTER THE CO-EFFICIENT OF x^2 : ");
scanf("%f",&a);

printf("ENTER THE CO-EFFICIENT OF x : ");
scanf("%f",&b);

printf("ENTER THE CONSTANT TERM : ");
scanf("%f",&c);

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

if(d<0)
{
printf("NO REAL ROOTS EXIST...\n");
d=(-d);
//root in the form of x + iy
x=(-b)/(2*a);
y=d/(2*a);
printf("THE ROOTS ARE COMPLEX...\n");
if(y>0)
{
printf("FIRST ROOT = %f +i%f\n",x,y);
printf("SECOND ROOT = %f -i%f",x,y);
}
else
{
y=(-y);
printf("FIRST ROOT = %f +i%f\n",x,y);
printf("SECOND ROOT = %f -i%f",x,y);

}
getch();
return 1;

}
else if(d==0)
printf("BOTH THE ROOTS ARE REAL AND EQUAL...\n");
else
printf("BOTH THE ROOOTS ARE REAL AND UNEQUAL...\n");

root1=((-b)+d)/(2*a);
root2=((-b)-d)/(2*a);

printf("FIRST ROOT = %f\n",root1);
printf("SECOND ROOT = %f",root2);

getch();
return 1;
}
/********************************************************************************/

No comments:

Post a Comment