Tweet
Share
COMPUTER PROGRAMMING
HOME
Quadratic equation
Floids Triangle
Fibonacci series & Prime numbers
Merge Sorting
Stack in an array
Two Stacks in an array
Tweet
Share
C PROGRAMMING QUESTIONS AND ANSWERS WITH ALGORITHM
1.Write a C Program to find the root of a Quadratic equation?
ALGORITHM
Step 1:Start
Step 2:Declare variables a,b,c,D,r1,r2,m,s
Step 3:Input the coefficients of x2,x and constant.
Step 4:Read the coefficients and constants :a,b and c respectively.
Step 5:Find determinant, D= (b*b)-(4*a*c).
Step 6:If D is greater than 0 then
6.1)Print the roots are real and distinct.
6.2)Find the root r1=((-b+pow(D,0.5))/2*a)
6.3) Find the root r2=((-b-pow(D,0.5))/2*a)
6.4)Print the roots r1 and r2.
Step 7:Else
7.1)If D is equal to 0 then
a)Print the real and equal.
b)Find the roots r1=r2=(-b/(2*a))
c) Print the roots r1 and r2.
7.2)Else
a)Assign D=-D
b) Print the roots are imaginary.
c)Find m=(-b/(2*a))
d)Find s=(pow(D,.5)/(2*a))
e)Print the roots m+is and m-is.
7.3)End if.
Step 8:End if.
Step 9:Stop.
PROGRAM
#include <stdio.h>
#include <conio.h>
#include< math.h>
void main()
{
float a,b,c,D,r1,r2,m,s;
clrscr();
printf("ENTER the coefficients of x2,x and constant\n");
scanf("%f%f%f",&a,&b,&c);
D=(b*b-4*a*c);
if(D>0)
{
printf("the roots are real and distinct");
r1=((-b+pow(D,0.5))/2*a);
r2=((-b-pow(D,0.5))/2*a);
printf("The roots are %f %f",r1,r2);
}
else if(D==0)
{
printf("The roots are real and equal \n");
r1=(-b/(2*a));
r2=r1;
printf("the roots are %f,%f",r1,r2);
}
else
{
D=-D;
printf("the roots are imaginary");
m=(-b/(2*a));
s=(pow(D,.5)/(2*a));
printf("the roots are %f+i%f and %f-i%f",m,s,m,s);
}
getch();
}
OUTPUT
ENTER the coefficients of x^2,x and constant
1 3 4
the roots are imaginary
the roots are -1.500000+i1.322876 and -1.500000-i1.322876
ENTER the coefficients of x2,x and constant
1 6 9
The roots are real and equal
the roots are -3.000000,-3.000000
Next>>