C program and output
/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
#include <math.h>
int main(void)
{
double a = 1.0, b = -3.0, c = 2.0;
if (a == 0.0) { puts("Not quadratic"); return 1; }
double d = b*b - 4.0*a*c;
if (d > 0.0) {
printf("Roots = %.2f and %.2f\n", (-b+sqrt(d))/(2*a), (-b-sqrt(d))/(2*a));
} else if (d == 0.0) {
printf("Repeated root = %.2f\n", -b/(2*a));
} else {
printf("Roots = %.2f +/- %.2fi\n", -b/(2*a), sqrt(-d)/fabs(2*a));
}
return 0;
}
Sample input and output
This example uses values already written in the program.
Output
Roots = 2.00 and 1.00
Related exam questions
Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.
- June 2022 · Q8(a) · 7 marks
Write an algorithm to find the roots of a quadratic equation.
Paper P03 · page 1 - July 2021 · Q8(a) · 7 marks
Write an algorithm for finding the roots of a quadratic equation.
Paper P04 · page 1 - March/April 2023 · Q3(a) · 4 marks
Write an algorithm to find the roots of a quadratic equation.
Paper P05 · page 1
View all matching exam references →