Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views2 pages

CP

This C program calculates the roots of a quadratic equation based on user-provided coefficients a, b, and c. It checks the discriminant to determine if the roots are real and distinct, real and the same, or complex. The program also handles the case where coefficient 'a' is zero, indicating that it's not a quadratic equation.

Uploaded by

Brice Nyonga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

CP

This C program calculates the roots of a quadratic equation based on user-provided coefficients a, b, and c. It checks the discriminant to determine if the roots are real and distinct, real and the same, or complex. The program also handles the case where coefficient 'a' is zero, indicating that it's not a quadratic equation.

Uploaded by

Brice Nyonga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>

#include <math.h>

int main() {

double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;

// Input coefficients

printf("Enter coefficients a, b and c: ");

scanf("%lf %lf %lf", &a, &b, &c);

// Calculate the discriminant

discriminant = b * b - 4 * a * c;

// Check if a is zero

if (a == 0) {

printf("Coefficient 'a' cannot be zero. Not a quadratic equation.\n");

return 1;

// Check the nature of the roots

if (discriminant > 0) {

// Two real and distinct roots

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Roots are real and different.\n");

printf("Root 1 = %.2lf\n", root1);

printf("Root 2 = %.2lf\n", root2);

} else if (discriminant == 0) {

// One real root


root1 = root2 = -b / (2 * a);

printf("Roots are real and the same.\n");

printf("Root 1 = Root 2 = %.2lf\n", root1);

} else {

// Complex roots

realPart = -b / (2 * a);

imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("Roots are complex and different.\n");

printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart);

printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart);

return 0;

You might also like