Enter quadratic equation in the format ax^2+bx+c: 2x^2+4x+-1 Roots of quadratic equation are: 0.000, -2.000 Other Related Programs in c C Program to calculate the Combinations and Permutations.
C BasicsC Programming TutorialData Structures |
- C Programming Exercises
C program to find all roots of a quadratic equation using switch case
- Quadratic Equation is ax^2+bx+c=0 Enter values of a,b and c:3 4 1. Two real and distinct roots Roots are -0.333333 and -1.
- One thought on “ C Program to Find Roots of Quadratic Equation ” Praveen October 21, 2018. Hi, I’ve a doubt in this program. When you find the value for imaginary root, why to use minus in square root (sqrt(-d))? Because the value which you get from d will already be in negative. Please make my doubt clear.
Write a C program to find all roots of a Quadratic equation using switch case. How to find all roots of a quadratic equation using switch case in C programming. Logic to calculate roots of quadratic equation in C program.
Example
Input
Input a: 4
Input b: -2
![How to do quadratic equations How to do quadratic equations](https://dvrtechnopark.files.wordpress.com/2015/02/37c79-c2btutorial.jpg?w=256)
Input c: -10
Output
Root1: 1.85
Root2: -1.35
Required knowledge
Basic C programming, Relational operators, Switch case statement
Quadratic equation
In elementary algebra quadratic equation is an equation in the form of
Solving quadratic equation
A quadratic equation can have either one or two distinct real or complex roots depending upon nature of discriminant of the equation. Where discriminant of the quadratic equation is given by
Depending upon the nature of the discriminant, formula for finding roots can be given as: Mahjong 247 summer.
Case 1: If discriminant is positive. Then there are two real distinct roots given by.
Case 2: If discriminant is zero. Then it have exactly one real root given by.
Case 3: If discriminant is negative. Then it will have two distinct complex roots given by.
Logic to find roots of quadratic equation using switch..case
Step by step descriptive logic to find roots of quadratic equation using switch case.
1.Input coefficients of quadratic equation. Store it in some variable say a, b and c.
2.Find discriminant of given equation using formula i.e. discriminant = (b * b) - (4 * a * c).
![C++ C++](https://2.bp.blogspot.com/-qAVjZov0mkk/V06yGfST5JI/AAAAAAAAAcE/nTFcaLE0rRgd2ld343fsYZT1Hlb2qFruACLcB/s1600/solution%2Bof%2Ba%2Bquadratic%2Bequation_000001.jpg)
You can also use pow() function to find square of b.
3.Compute the roots based on the nature of discriminant. Switch the value of switch(discriminant > 0).
4.The expression (discriminant > 0) can have two possible cases i.e. case 0 and case 1.
5.For case 1 means discriminant is positive. Apply formula root1 = (-b + sqrt(discriminant)) / (2*a); to compute root1 and root2 = (-b - sqrt(discriminant)) / (2*a); to compute root2.
6.For case 0 means discriminant is either negative or zero. There exist one more condition to check i.e. switch(discriminant < 0).
7.Inside case 0 switch the expression switch(discriminant < 0).
8.For the above nested switch there are two possible cases. Which is case 1 and case 0. case 1 means discriminant is negative. Whereas case 0 means discriminant is zero.
9.Apply the formula to compute roots for both the inner cases.
Program to find roots of quadratic equation using switch..case
Output
C Program To Find Roots Of Quadratic Equation With Flow Chart
Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 4 -2 -10
Two distinct and real roots exists: 1.85 and -1.35
- Related Questions & Answers
- Selected Reading
C++ProgrammingServer Side Programming
C Program To Find Roots Of Quadratic Equation Using Pointers
A quadratic equation is in the form ax2 + bx + c. The roots of the quadratic equation are given by the following formula −
There are three cases −
b2 < 4*a*c - The roots are not real i.e. they are complex
b2 = 4*a*c - The roots are real and both roots are the same.
b2 > 4*a*c - The roots are real and both roots are different
The program to find the roots of a quadratic equation is given as follows.
Example
Output
C++ Quadratic Equation Solver
In the above program, first the discriminant is calculated. If it is greater than 0, then both the roots are real and different.
This is demonstrated by the following code snippet.
If the discriminant is equal to 0, then both the roots are real and same. This is demonstrated by the following code snippet.
If the discriminant is less than 0, then both the roots are complex and different. This is demonstrated by the following code snippet.