We can further enhance the program to prompt the input values of a, b, c to the user. Right now we have to modify the code for the values. Generate the equation string based on the values of a, b, and c and display the equation string in the output. Take into consideration the imaginary roots if discriminant is less than 0. C if.else Statement The standard form of a quadratic equation is: ax 2 + bx + c = 0, where a, b and c are real numbers and a!= 0 The term b 2 -4ac is known as the discriminant of a quadratic equation.
- C Program To Find Quadratic Equation Using Switch Case
- C Program To Find Discriminant Of Quadratic Equation
- C Program To Find Quadratic Equation Of A Number
- Related Questions & Answers
- Selected Reading
We can apply the software development method to solve the linear equation of one variable in C programming language.
Requirement
- The equation should be in the form of ax+b=0
- a and b are inputs, we need to find the value of x
Analysis
Here,
- An input is the a,b values.
- An output is the x value.
Algorithm
Refer an algorithm given below to find solution of linear equation.
Program
Following is the C program to find the solution of linear equation −
C Program To Find Quadratic Equation Using Switch Case
Output
When the above program is executed, it produces the following result −
C program to find roots of a quadratic equation: Coefficients are assumed to be integers, but roots may or may not be real.
For a quadratic equation ax2+ bx + c = 0 (a≠0), discriminant (b2-4ac) decides the nature of roots. If it's less than zero, the roots are imaginary, or if it's greater than zero, roots are real. If it's zero, the roots are equal.
C Program To Find Discriminant Of Quadratic Equation
For a quadratic equation sum of its roots = -b/a and product of its roots = c/a. Let's write the program now.
Roots of a Quadratic equation in C
#include <stdio.h>#include <math.h>
int main()
{
int a, b, c, d;
double root1, root2;
printf('Enter a, b and c where a*x*x + b*x + c = 0n');
scanf('%d%d%d',&a,&b,&c);
d = b*b -4*a*c;
C Program To Find Quadratic Equation Of A Number
if(d <0){// complex roots, i is for iota (√-1, square root of -1)
printf('First root = %.2lf + i%.2lfn',-b/(double)(2*a),sqrt(-d)/(2*a));
printf('Second root = %.2lf - i%.2lfn',-b/(double)(2*a),sqrt(-d)/(2*a));
}
else{// real roots
root1 =(-b +sqrt(d))/(2*a);
root2 =(-b -sqrt(d))/(2*a);
printf('First root = %.2lfn', root1);
printf('Second root = %.2lfn', root2);
}
return0;
}