C Program if else : "Calculate roots of a quadratic equation" with output

 #include <stdio.h>

#include <math.h> int main() { double a, b, c, discriminant, root1, root2; printf("Enter coefficients a, b, and c of the quadratic equation (ax^2 + bx + c): "); scanf("%lf %lf %lf", &a, &b, &c); // Calculate the discriminant discriminant = b * b - 4 * a * c; if (discriminant > 0) { // Two real and distinct roots root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("Root1 = %.2lf and Root2 = %.2lf\n", root1, root2); } else if (discriminant == 0) { // One real root (two equal roots) root1 = -b / (2 * a); printf("Root1 = Root2 = %.2lf\n", root1); } else { // Complex roots double realPart = -b / (2 * a); double imaginaryPart = sqrt(-discriminant) / (2 * a); printf("Root1 = %.2lf + %.2lfi and Root2 = %.2lf - %.2lfi\n", realPart, imaginaryPart, realPart, imaginaryPart); } return 0; }






Enter coefficients a, b, and c of the quadratic equation (ax^2 + bx + c): 1 -3 2 Root1 = 2.00 and Root2 = 1.00

Comments

Popular posts from this blog

Video Formats

Where video editing is used

File saving and folder management