Write a program to compute the real roots of a quadratic equation
ax2 + bx + c = 0
The roots are given by the equations
x1 = -b + sqrt(b2-4ac)/2a
x2 = -b - sqrt(b2-4ac)/2a
The program should request for the values of the constants a, b and c and print the values of x1 and x2. Use the following rules:
(a) No solution, if both a and b are zero
(b) There is only one root, if a = 0 (x = -c/b)
(c) There are no real roots, if b2 -4ac is negative
(d) Otherwise, there are two real roots
Test your program with appropriate data so that all logical paths are working as per your design. Incorporate appropriate output messages.
#include <stdio.h>
void main()
{
float x1,x2,a,b,c,d;
clrscr();
printf("Enter the value of a,b,c to compute the root of quadratic equation:");
scanf("%f %f %f",&a,&b,&c);
d=b*b-4*a*c;
x1=-b+sqrt(d)/2*a;
x2=-b-sqrt(d)/2*a;
if(a==0&&b==0)
printf("No solution Available!\b");
else
if(a==0&&b!=0)
printf("There is only one root X= -c/b");
else
if(d==0)
printf("There are no real roots!");
else
printf("x1=%f\nx2=%f",x1,x2);
getch();
}
ax2 + bx + c = 0
The roots are given by the equations
x1 = -b + sqrt(b2-4ac)/2a
x2 = -b - sqrt(b2-4ac)/2a
The program should request for the values of the constants a, b and c and print the values of x1 and x2. Use the following rules:
(a) No solution, if both a and b are zero
(b) There is only one root, if a = 0 (x = -c/b)
(c) There are no real roots, if b2 -4ac is negative
(d) Otherwise, there are two real roots
Test your program with appropriate data so that all logical paths are working as per your design. Incorporate appropriate output messages.
#include <stdio.h>
void main()
{
float x1,x2,a,b,c,d;
clrscr();
printf("Enter the value of a,b,c to compute the root of quadratic equation:");
scanf("%f %f %f",&a,&b,&c);
d=b*b-4*a*c;
x1=-b+sqrt(d)/2*a;
x2=-b-sqrt(d)/2*a;
if(a==0&&b==0)
printf("No solution Available!\b");
else
if(a==0&&b!=0)
printf("There is only one root X= -c/b");
else
if(d==0)
printf("There are no real roots!");
else
printf("x1=%f\nx2=%f",x1,x2);
getch();
}
No comments:
Post a Comment