An interactive C program to find the roots of a Quadratic equation

By | August 13, 2013

First thing to know before writing this program is the Quadratic Equation Formula.

Quadratic_Eqn

Here the problem is the this whole Quadratic Equation can not be written on single line.

Now, find out the variables needed for calculating Roots of Quadratic Equation.

X1 = (-B-Square Root(b 2 – 4AC))/2A    AND   X2 = (-B+Square Root(b 2 – 4AC))/2A

By looking at the formula, we have A,B,C,R1,R2, we need two more for splitting the Equation into parts, So we need PART,EQN. (i.e. in all SEVEN Variables)

Now decide the data type.

int, float, and double are the choices for the variable to be used, since we are using only fraction number and not the whole number due to the complexity of the Quadratic Equation. Hence we will omit int. Select float as our data type.

float A,B,C,R1,R2,PART,EQN;

data type and variables selection done.

Interactive means the program will not have any hardcoded values assigned to variables but user has to provide them.

To get values from the user screen we have a function called scanf() which will help us to scan values entered by the user by keyboard and visible on the screen.

We cannot expect that user should enter values in the sequence blindly without knowing what to enter. For that purpose we should guide the user to enter expected values by displaying message.

(E.g. ENTER THE VALUE OF A : ) than scan the value entered for A, B and C.

X1 = (-B-Square Root(b 2 – 4AC))/2A    AND   X2 = (-B+Square Root(b 2 – 4AC))/2A

This formula has a Raise to 2 this can be achieved by using a inbuilt function called pow() function which is there in another header file called math.h So we have to INCLUDE math.h in our program.

e.g.  XY can be written as pow(X,Y) and So.

This Quadratic Equation formula cannot be written on a single line statement. So we write it in several statements as:

Here the problem is that how to calculate Square Root, This can be achieved by using a inbuilt function called sqrt() function which is there in math.h, Now problem with sqrt() function is we cannot pass negative value to it otherwise it gives an exception(ERROR). This problem can be solved by using a inbuilt function called abs() function which is a absolute function which passes only Absolute (Positive) value to the sqrt() function. If the value which is passed is negative value then it’s Absolute (Positive) value has to be assigned a negative value after the result is obtained.

EQN=pow(B,2)-(4*A*C);
if(EQN<0)
{
    PART=sqrt(abs(EQN));
    PART=-PART;
}
else
{
   PART=sqrt(EQN);
}
R1=(-B+PART)/(2*A);
R2=(-B-PART)/(2*A);

 

Here we are using %.2f instead of %f so that the value is Printed to Two Decimal Digits.

Note:- Remember whenever you are using sqrt() function be careful to  pass positive value or if you are not sure than pass the value by Absolute function inside instead of passing a negative value unknowingly.

C program code:

  1. #include<stdio.h>
  2. #include<math.h>
  3. void main()
  4. {
  5. float A,B,C,R1,R2,PART,EQN;
  6. clrscr();
  7. printf("ENTER THE VALUE OF A : ");
  8. scanf("%f",&A);
  9. printf("ENTER THE VALUE OF B : ");
  10. scanf("%f",&B);
  11. printf("ENTER THE VALUE OF C : ");
  12. scanf("%f",&C);
  13. EQN=pow(B,2)-(4*A*C);
  14. if(EQN<0)
  15. {
  16. PART=sqrt(abs(EQN));
  17. PART=-PART;
  18. }
  19. else
  20. {
  21. PART=sqrt(EQN);
  22. }
  23. R1=(-B+PART)/(2*A);
  24. R2=(-B-PART)/(2*A);
  25. printf("THE CALCULATED ROOTS ARE : %.2f AND %.2f",R1,R2);
  26. getch();
  27. }

Screen Shots:-

C_program_Quadratic

C_program_Quadratic_Output

Leave a Reply