0% found this document useful (0 votes)
11 views2 pages

Quadratic Equation

pop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Quadratic Equation

pop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

.

Develop a program to compute the roots of a quadratic equation by accepting the

coefficients. Print appropriate messages.

Algorithm:

Step 1: Start

Step 2: Read the values of non-zero coefficients a,b and c.

Step 3: Compute the value of discriminant (disc) which is equal to (b*b-4*a*c).

Step 4: Check if disc is equal to 0. If true, then go to Step 5. Otherwise, go to Step 6.

Step 5: Compute the roots.

root1 = (-b)/ (2*a) , root2=root1

Output the values of roots, root1 and root2. Go to Step 9.

Step 6: Check if disc is greater than zero or not. If true, then go to Step 7. Otherwise, go to

Step 8.

Step 7: Compute the real and distinct roots, root1 = (-b+sqrt(disc))/(2*a)

root2 = (-b-sqrt(disc))/(2*a)

Output the values of roots, root1 and root2. Go to Step 9.

Step 8: Compute the complex and distinct roots. Compute the real part, r_part = (-b)/(2*a)

Compute the imaginary part, i_part = sqrt(-disc)/(2*a)

Output roots as root1 = r_part + i_part

root2 = r_part – i_part

Step 9: Stop

Program

#include<stdio.h>
#include<math.h>
void main()
{
float a, b, c, root1, root2, r_part, i_part, disc;
printf("Enter the 3 non-zero coefficients\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0 || b==0 || c==0)
{
printf(“Co-efficients cannot be 0\n”);
return;
}
disc=(b*b)-(4*a*c);
if(disc==0)
{
printf("Roots are equal\n");
root1=-b/(2*a);
root2=root1;
printf("root1=root2=%f",root1);
}
else if(disc>0)
{
printf("Roots are real and distinct\n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("root1 = %f \t root2 = %f\n",root1,root2);
}
else
{
printf("Roots are complex\n");
r_part=-b/(2*a);
i_part=(sqrt(-disc))/(2*a);
printf("root1 = %f+i%f \n root2 = %f-i%f\n",r_part,i_part,r_part,i_part);
}
}

You might also like