C Program To Find All Roots of A Quadratic Equation Using Switch Case - Codeforwin
C Program To Find All Roots of A Quadratic Equation Using Switch Case - Codeforwin
Learn C programming, Data Structures tutorials, exercises, examples, programs, hacks, tips and tricks online.
Programming Languages
Explore remote-first hiring
HackerRank vs DevSkiller IT testing
software | Tool reviews & comarission
devskiller.com
OPEN
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
Input c: -10
Output
Root1: 1.85
Root2: -1.35
Required knowledge
https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 1/6
1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin
Quadratic equation
In elementary algebra quadratic equation is an equation in the form of
Depending upon the nature of the discriminant, formula for finding roots can be given as:
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.
https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 2/6
1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin
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 .
6. For case 0 means discriminant is either negative or zero. There exist one more condition to
check i.e. 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.
Internships In 70+
Countries
Graduate With Up To One Year Of Work
Experience With ALU’s Global Internship
Program.
1 /**
2 * C program to find all roots of a quadratic equation using switch case
3 */
4
5 #include <stdio.h>
6 #include <math.h> /* Used for sqrt() */
7
8 int main()
9 {
0 fl t b
https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 3/6
1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin
10 float a, b, c;
11 float root1, root2, imaginary;
12 float discriminant;
13
14 printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c
15 scanf("%f%f%f", &a, &b, &c);
16
17 /* Calculate discriminant */
18 discriminant = (b * b) - (4 * a * c);
19
20
21 /* Compute roots of quadratic equation based on the nature of discri
22 switch(discriminant > 0)
23 {
24 case 1:
25 /* If discriminant is positive */
26 root1 = (-b + sqrt(discriminant)) / (2 * a);
27 root2 = (-b - sqrt(discriminant)) / (2 * a);
28
29 printf("Two distinct and real roots exists: %.2f and %.2f",
30 root1, root2);
31 break;
32
33 case 0:
34 /* If discriminant is not positive */
35 switch(discriminant < 0)
36 {
37 case 1:
38 /* If discriminant is negative */
39 root1 = root2 = -b / (2 * a);
40 imaginary = sqrt(-discriminant) / (2 * a);
41
42 printf("Two distinct complex roots exists: %.2f + i%
43 root1, imaginary, root2, imaginary);
44 break;
45
46 case 0:
47 /* If discriminant is zero */
48 root1 = root2 = -b / (2 * a);
49
50 printf("Two equal and real roots exists: %.2f and %.
51
52 break;
53 }
54 }
55
56 return 0;
57 }
Output
https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 4/6
1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin
Happy coding 😉
Recommended posts
Switch case programming exercise index
C program to print day of week name using switch case.
C program to find total number of days in a month using switch case.
C program to check positive, negative or zero using switch case.
C program to find maximum between two numbers using switch case.
C program to check whether a number is even or odd using switch case.
C program to create simple calculator using switch case.
About Pankaj
Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write
programming articles especially for beginners. He works at Vasudhaika Software Sols. as a Software
Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and
Music lover.
https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 5/6
1/13/22, 8:02 AM C program to find all roots of a quadratic equation using switch case - Codeforwin
https://codeforwin.org/2016/04/c-program-to-find-all-roots-of-quadratic-equation-using-switch.html 6/6