Quadratic Formula in C language

Let's learn today, how we can implement it in c language.
First, we'll take variables a, b and c for the quadratic formula values.
Pos and Neg variable for negative and postive value of quadratic formula.
Then, we'll take the values of a,b, and c as input from the user.
After that, we'll calculate '4' into 'a' into 'c' i.e (4 * a * c) and save the answer in d variable and apply the condition value of d is greater than 0 than continue, else show complex roots.
In if condition we'll apply the formula to calculate the value of quadratic value. and save the answer in their respective variable.
After that, we'll print the answers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
void main() | |
{ | |
float a, b, c, d, pos, neg; | |
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); | |
d = (4 * a * c); | |
if( d > 0) // applying condition if d is greater than 0 | |
{ | |
pos = ((-b) + (sqrt( (pow(b,2)) - (d) ) )) / (2*a); | |
neg = ((-b) - (sqrt( (pow(b,2)) - (d) ) )) / (2*a); | |
printf("Root1 = %f \n", pos); | |
printf("Root2 = %f \n", neg); | |
} | |
else | |
{ | |
printf("roots are complex"); | |
} | |
} |
Comments
Post a Comment