Write a C program to find the roots of a quadratic equation
.
Description:
Nature of roots of quadratic equation can be known from the quadrant = b2-4ac
If b2-4ac >0 then roots are real and unequal
If b2-4ac =0 then roots are real and equal
If b2-4ac <0 then roots are imaginary
Algorithm:
Step 1: Start Step 2: Read A, B, C as integer Step 3: Declare disc, deno, x1, x2 as float Step 4: Assign disc = (B * B) - (4 * A * C) Step 5: Assign deno = 2 * A; Step 6: if( disc > 0 ) begin Print “THE ROOTS ARE REAL ROOTS” Assign x1 ← (-B / deno) + (sqrt(disc) / deno) Assign x2 ← (-B / deno) - (sqrt(disc) / deno) Print x1, x2 end else if(disc = 0) begin Print “ THE ROOTS ARE REPEATED ROOTS" Assign x1 ← -B / deno Print x1 end else Print “THE ROOTS ARE IMAGINARY ROOTS” Step7: Stop
Flowchart:
Program:
#include<stdio.h> #include<conio.h> #include<math.h> void main() { float a, b, c, d, root1, root2; clrscr(); printf("Enter the values of a, b, c\n"); scanf("%f%f%f", &a, &b, &c); if(a == 0 || b == 0 || c == 0) { printf("Error: Roots can't be determined"); } else { d = (b * b) - (4.0 * a * c); if(d > 0.00) { printf("Roots are real and distinct \n"); root1 = -b + sqrt(d) / (2.0 * a); root2 = -b - sqrt(d) / (2.0 * a); printf("Root1 = %f \nRoot2 = %f", root1, root2); } else if (d < 0.00) { printf("Roots are imaginary"); root1 = -b / (2.0 * a) ; root2 = sqrt(abs(d)) / (2.0 * a); printf("Root1 = %f +i %f\n", root1, root2); printf("Root2 = %f -i %f\n", root1, root2); } else if (d == 0.00) { printf("Roots are real and equal\n"); root1 = -b / (2.0 * a); root2 = root1; printf("Root1 = %f\n", root1); printf("Root2 = %f\n", root2); } } getch(); }
Input & Output:
Enter the values of a, b, c 1 2 3 Roots are imaginary Root1 = -1.000 + i Root2 = -1.000 - i
Viva Questions:
Q: What are various types of loop statements?
Ans: While, do- while, for loop statements.
Q: What is the difference between while and do-while statements?
Ans: In while the condition will be checked first and then enter into a loop. But in do- while the statements will be executed first and then finally check the Condition.
Q: How to find the roots of qudratric equtations?
Ans: Nature of roots of quadratic equation can be known from the quadrant = b2-4ac
- If b2-4ac >0 then roots are real and unequal
- If b2-4ac =0 then roots are real and equal
- If b2-4ac <0 then roots are imaginary
Q: List out the C features?
Ans: Portability,flexibility, wide acceptability etc..,
-
UpdatedDec 24, 2014
-
Views114,645
Write a C program to find the roots of a quadratic equation
.
Write a C program to count the lines, words and characters in a given text.
Write a C program to find the sum of individual digits of a positive integer.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use switch statement
)
Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Write C programs that use both recursive
and non-recursive functions
- To find the
factorial
of a given integer. - To find the
GCD
(greatest common divisor) of two given integers.