Blogger templates

Pages

Saturday, 16 June 2012

Write a C Program to Find the Roots of a Quadratic Equation by using if-else and else-if Statement

Quadratic Equation

Statement of C Program: This Program finds all the Roots of a Quadratic Equation, for non-zero cofficients:

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float A , B , C , root1 , root2;
float realp , imgp , disc;               
                                      /* realp stands for Real Part and imgp stands for Imaginary Part */
clrscr();
printf(" Enter the values of A , B and C\n");
scanf("%f%f%f" , &A , &B , &C);

if(A==0 || B==0 || C==0)
{
printf(" Error: Roots cannot be determined\n");
}

else
{
dis = (B * B) - (4 * A * C);
if(disc<0)
{
printf(" Imaginary Roots\n");
realp = -B/(2*A);
imagp = sqrt(abs(disc))/(2*A);
printf(" Root1 = %f + i%f\n" , realp , imagp);
printf(" Root2 = %f - i%f\n" , realp , imagp);
}

else if( disc == 0)
{
printf(" Roots are real and equal\n");
root1 = -B/(2 * A);
root2 = Root1;
printf(" Root1 = %f\n" , root1);
printf(" Root2 =%f\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 = %f\n" , root1);
printf(" Root2 = %f\n" , root2);
}

}
}                                               /*  End of main()  */


Output1:
Enter the values of A , B and C
3 2 1
Imaginary roots
Root1 = -0.3333 + i0.471402
Root2 = -0.3333 - i0.471405


Output2:
Enter the values of A , B and C
1 2 1
Roots are real and equal
Root1 = -1.0000
Root2 = -1.0000


Output3:
Enter the values of A , B and C
3 5 2
Roots are real and distinct
Root1 = -0.666667
Root2 = -1.000000

                 
                                                              That's All !
                                   

0 comments:

Post a Comment