Thursday 21 May 2015

Solve quadratic equations given the value of a, b and c.

#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float d,r1,r2,imag;
printf("enter the value of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d>0)
{
printf("roots are real\n");
r1=((-b)+sqrt(d))/(2*a);
r2=((-b)-sqrt(d))/(2*a);
printf("roots are real %f%f\n",r1,r2);
}
else if(d==0)
{
printf("roots are equal\n");
r1=(-b)/(2*a);
r2=r1;
printf("roots are %f%f",r1,r2);
}
else
{
printf("\n roots are imaginary");
r1=(-b)/(2*a);
imag=sqrt(fabs(-d))/(2*a);
printf("\n roots are %f +i %f, %f -i %f",r1,imag,r2,imag);
}
getch();
}

No comments:

Post a Comment