Program to find the roots of a quadratic equation
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, d, real, imag, r1, r2, n;
int k ;
clrscr() ;
printf("Enter the values of A, B & C : ");
scanf("%f %f %f", &a, &b, &c);
if(a != 0)
{
d = b * b - 4 * a * c;
if(d < 0)
k = 1;
if(d == 0)
k = 2;
if(d > 0)
k = 3;
switch(k)
{
case 1 :
printf("\nRoots are imaginary\n");
real = - b / (2 * a);
d = - d;
n = pow((double) d, (double) 0.5);
imag = n / (2 * a);
printf("\nr1 = %7.2f j%7.2f", real, imag);
printf("\nr2 = %7.2f - j%7.2f", real, imag);
break;
case 2 :
printf("\nRoots are real and equal\n");
r1 = - b / (2 * a);
printf("\nr1 = r2 = %7.2f", r1);
break;
case 3 :
printf("\nRoots are real and unequal\n");
r1 = (- b sqrt((double) d)) / (2 * a);
r2 = (- b - sqrt((double) d)) / (2 * a);
printf("\nr1 = %7.2f",r1);
printf("\nr2 = %7.2f",r2);
break;
}
}
else
printf("\nEquation is linear");
getch();
}
Output:
Case: 1
Enter the values of A, B & C : 0.0 4.0 7.0
Equation is linear
Case: 2
Enter the values of A, B & C : 1.0 2.0 7.0
Roots are imaginary
r1 = -1.00 + j 2.45
r2 = -1.00 - j 2.45
Case: 3
Enter the values of A, B & C : 1.0 2.0 1.0
Roots are real and equal
r1 = r2 = -1.00
Case: 4
Enter the Values of A, B & C : 2.0 7.0 1.0
Roots are real and unequal
r1 = -0.15
r2 = -3.35
-
UpdatedDec 30, 2019
-
Views7,208
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order