Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Create a program that solves a quadratic equation (ax^2 + bx + c = 0) for given coefficients a, b, and c. Handle different cases (real roots, complex roots).

#include <stdio.h>
#include <math.h>

int main() {
// Declare variables
float a, b, c, discriminant, root1, root2;

// Input: Get coefficients from the user
printf(“Enter coefficients (a, b, c) of the quadratic equation (ax^2 + bx + c): “);
scanf(“%f %f %f”, &a, &b, &c);

// Process: Calculate discriminant
discriminant = b * b – 4 * a * c;

// Check the nature of roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b – sqrt(discriminant)) / (2 * a);
printf(“Roots are real and different: %.2f and %.2fn”, root1, root2);
} else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf(“Roots are real and equal: %.2fn”, root1);
} else {
float realPart = -b / (2 * a);
float imaginaryPart = sqrt(-discriminant) / (2 * a);
printf(“Roots are complex and different: %.2f + %.2fi and %.2f – %.2fin”, realPart, imaginaryPart, realPart, imaginaryPart);
}

return 0; // Exit the program successfully
}