0% found this document useful (0 votes)
2 views3 pages

Program 2

The document outlines an algorithm and C program to compute the roots of a quadratic equation based on user-provided coefficients A, B, and C. It handles three cases: non-determinant roots when A, B, and C are all zero, real and equal roots when the discriminant is zero, and real and distinct roots when the discriminant is positive, with complex roots when the discriminant is negative. The program includes input prompts and appropriate messages for each case of roots.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Program 2

The document outlines an algorithm and C program to compute the roots of a quadratic equation based on user-provided coefficients A, B, and C. It handles three cases: non-determinant roots when A, B, and C are all zero, real and equal roots when the discriminant is zero, and real and distinct roots when the discriminant is positive, with complex roots when the discriminant is negative. The program includes input prompts and appropriate messages for each case of roots.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Program 2:

Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate
messages.

Algorithm
Step-1: [Read values of A, B, C]-Read A, B, C.
Step-2: [Check for Co-efficient]-If A=B=C=0 then roots are non-determinant
Step-3:[Compute the value Disc]- Disc=B*B-4*A*C;
Step-4:[Different roots are obtained depending on the value of disc]-
If Disc is lesser than 0 than goto step 5
If Disc is equals to 0 than goto step 6
If Disc is greater than 0 than goto step 7.
Step-5: Print Imaginary roots.
Realp=-B/2*A;
Imagp=sqrt(Disc)/2*A;
Root1=realp + imagep;
Root2 = realp – imagep;
Step-6:Roots are real and equal
Root1=B/2*A;
Root2 = root1;
Step-7: roots are real and distinct
Root1= – B +sqrt(Disc)/2*A;
Root2 = -B -sqrt(Disc)/2*A;
Step-8: Stop
Flow Chart

Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
float a,b,c,disc;
float root1,root2,realp,imagp;
printf("Enter values of a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("roots cannot be determined\n");
exit(0);
}
disc=b*b-4*a*c;
if(disc==0)
{
printf("roots are real and equal\n");
root1=-b/(2*a);
root2=root1;
printf("root1=%.2f\n",root1);
printf("root2=%.2f\n",root2);
}
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=%.2f\n",root1);
printf("root2=%.2f\n",root2);
}

else
{
realp=-b/(2*a);
imagp=sqrt(fabs(disc))/(2*a);
printf("roots are complex\n");
printf("root1=%.2f+i%.2f\n",realp,imagp);
printf("root2=%.2f-i%.2f\n",realp,imagp);
}
}

You might also like