Quadratic Equation

  • Program to find the root of a Quadratic equation
                                              In Mathematics, Quadratic Equation is an equation of degree 2. The generalized form of a quadratic equation is, ax2 + bx + c = 0 where, x is an unknown value and a, b, and c are known values. We need to find the roots of this equation. First of all, lets formulate the mathematical equation for finding the roots (means the value of the unknown parameter x using the known values of a, b and c) of the above equation,
x = (-b ± √(b2-4ac)) / 2a
So we get two values for x, (-b + √(b2-4ac))/2a and (-b - √(b2-4ac))/2a and lets name this as r1 and r2.
The value (b2-4ac) is called the discriminant of the equation, let’s name this as D. So, D = b2-4ac and the equation becomes,
r1 = (-b + √D)/2a and r2 = (-b - √D)/2a.
Let see how this represented programmatically,
D = b * b - 4ac
r1 = -b/2a + pow(D,0.5)/2a    
r2 = -b/2a - pow(D,0.5)/2a

Case 1: D = 0
Therefore, the roots are r1 = -b/2a and r2 = -b/2a, which means that the roots are real and equal.
Eg: x2 + 2x + 1 = 0
 where a = 1, b =2, and c = 1.
D = 2 * 2 – 4 * 1 * 1 = 4 – 4 = 0
Therefore, r1 = r2 = -2/2*a = -2/2 = -1

Case 2: D ≠ 0
This means the roots are not equal and remains as imaginary.
Eg: x2 + x + 1 = 0
Where a = 1, b = 1 and c = 1.
D = 1 * 1 – 4 * 1 * 1 = 2 – 4 = -2
Therefore the roots are r1 = (-1 + √-2)/2 = -1 + i√2 and r2 = (-1 - √-2)/2 = -1 - i√2.
Where i =  √-1, representing an imaginary value.

Algorithm

step 1: Start
step 2: Declare variables a, b, c, D, r1, r2, m, s.
step 3: Input the coefficients of x2,x and constant.
step 4:Read the coefficients and constants: a, b and c respectively.
step 5: Find discriminant, D= (b*b) - (4*a*c).
step 6: If D is greater than 0 then
                                  6.1)Print the roots are real and distinct.
                                  6.2)Find the root r1 = ((-b+pow(D,0.5))/2*a)
                                  6.3) Find the root r2 = ((-b-pow(D,0.5))/2*a)
                                  6.4)Print the roots r1 and r2.
step 7: Else
                                  7.1)If  D is equal to 0 then
                                                a)Print the real and equal.
                                                b)Find the roots r1 = r2 = (-b/(2*a))
                                                c) Print the roots r1 and r2.
                                  7.2)Else
                                                a)Assign D = -D
                                                b) Print the roots are imaginary.
                                                c)Find m = (-b/(2*a))
                                                d)Find s = (pow(D,.5)/(2*a))
                                                e)Print the roots m+is and m-is.
                                  7.3)End if.
Step 8: End if.
Step 9: Stop.

Program

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() {
            float a,b,c,D,r1,r2,m,s;
            clrscr();
            printf("ENTER the coefficients of x2,x and constant\n");
            scanf("%f%f%f",&a,&b,&c);
            D=(b*b-4*a*c);
            if(D>0) {
                        printf("the roots are real and distinct");
                        r1=((-b+pow(D,.5))/2*a);
                        r2=((-b-pow(D,.5))/2*a);
                        printf("The roots are %f %f",r1,r2);
            } else if(D==0) {
                          printf("The roots are real and equal \n");
                          r1=(-b/(2*a));
                          r2=r1;
                          printf("the roots are %f,%f",r1,r2);
            } else {
                         D=-D;
                         printf("the roots are imaginary");
                         m=(-b/(2*a));
                         s=(pow(D,.5)/(2*a));
                         printf("the roots are %f+i%f and %f-i%f",m,s,m,s);
            }
            getch();
}

Output

ENTER the coefficients of x2,x and constant
1    3   4                                                                          
the roots are imaginary
the roots are -1.500000+i1.322876 and -1.500000-i1.322876
                                                                                
ENTER the coefficients of x2,x and constant
1   6   9                                                                          
The roots are real and equal                                                   
the roots are -3.000000,-3.000000                                              
                            

No comments:

Post a Comment