C009 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.

[codesyntax lang="c"]

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

[/codesyntax]

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.

[codesyntax lang=”c”]

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);

[/codesyntax]

 

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:

[codesyntax lang=”c” lines=”normal”]

#include<stdio.h>
#include<math.h>
void main()
{
float A,B,C,R1,R2,PART,EQN;
clrscr();
printf("ENTER THE VALUE OF A : ");
scanf("%f",&A);
printf("ENTER THE VALUE OF B : ");
scanf("%f",&B);
printf("ENTER THE VALUE OF C : ");
scanf("%f",&C);
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);
printf("THE CALCULATED ROOTS ARE : %.2f AND %.2f",R1,R2);
getch();
}

[/codesyntax]

Screen Shots:-

C_program_Quadratic

C_program_Quadratic_Output

 

Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.

Leave a Reply