Roots of Quadratic Equation
Algorithm
Input Coefficient of quadratic equation Output Roots complexity O(n). Roots (a,b,c) 1. Initialize root1:=root2:=0. 2. Set cal := (sqrt((b*b)-(4*a*c)))/(2*a). 3. Set root1 : = -b + cal. 4. Set root2 : = -b – cal. 5. If(root1<0 or root2<0) then print : “Imaginary roots†6. Else return 7. end
Algorithm Description
Roots() function finds root of quadratic equation where a , b and c are coefficient.
C Code
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,d;
float x,y;
printf("Enter cofficients of a*x^2 , b*X^1 and c\n");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
x=(-b+sqrt(d))/2*a;
y=(-b-sqrt(d))/2*a;
if(d>0)
{
printf("Roots are real");
}
else if(d==0)
{
printf("Roots are equal");
}
else
printf("Root are imagnary");
printf("\nRoots are %.2f and %.2f ",x,y);
return 0;
}