Logic:
- Find the discriminant as d = b*b-4*a*c
- If d>0 The equation have real and unequal roots.
- if d=0 The equation have real and equal roots
- if d<0 The equation have imaginary roots
Algorithm
- Enter coefficients of x2, x and constant terms a,b,c
- d=(b*b)-(4*a*c);
- if(d==0)
- printf(“equation have real and equal roots\n”);
- else if (d>0)
- printf(“equation have real and unequal roots\n”);
- else
- printf(“equation have imaginary roots”);
Program
#include <stdio.h>
int main()
{
printf("****************************************************");
printf("\n****************************************************");
printf("\n** WAP to find roots of a quadratic equation in C **");
printf("\n** Created by Sheetal Garg **");
printf("\n** Assistant Professor **");
printf("\n** Phone No:9467863365 **");
printf("\n****************************************************");
printf("\n****************************************************\n");
int a,b,c,d;
printf("enter the coefficient of x2, x and constant term\n");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d==0)
printf("equation have real and equal roots\n");
else if (d>0)
printf("equation have real and unequal roots\n");
else
printf("equation have imaginary roots");
return 0;
}
Output 1:
**************************************************** **************************************************** ** WAP to find roots of a quadratic equation in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************** **************************************************** enter the coefficient of x2, x and constant term 1 4 4 equation have real and equal roots
Output 2:
**************************************************** **************************************************** ** WAP to find roots of a quadratic equation in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************** **************************************************** enter the coefficient of x2, x and constant term 1 4 8 equation have imaginary roots
Output 3:
**************************************************** **************************************************** ** WAP to find roots of a quadratic equation in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************** **************************************************** enter the coefficient of x2, x and constant term 1 -3 2 equation have real and unequal roots