Tuesday, 8 March 2011

Write a program to find the roots of an equation ax2 + bx + c = 0

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b,c,alf,bt,dlt;
clrscr();
printf("\n Enter a: ");
scanf("%f",&a);
printf("\n Enter b: ");
scanf("%f",&b);
printf("\n Enter c: ");
scanf("%f",&c);
dlt=b*b-4*a*c;
if(dlt==0)
{
printf("\n ALPHA=BETA=%f",-b/(2*a));
}
else if(dlt<0)
{
printf("\n Imaginary Roots");
}
else
{
alf=(-b+sqrt(dlt))/(2*a);
bt=(-b-sqrt(dlt))/(2*a);
printf("\n\n Alpha = %f\n Beta=%f\n",alf,bt);
}
getch();
return 0;
}
Output:
(1)
Enter a: 12
Enter b: 2
Enter c: 34
Imaginary Roots
(2)
Enter a: 2
Enter b: 6
Enter c: 2
Alpha = -0.381966
Beta = -2.618034
(3)
Enter a: 2
Enter b: 4
Enter c: 2
ALPHA=BETA= -1.000000

No comments:

Post a Comment