Coding Practice

Write a C program to find all roots of a quadratic equation

Find All Roots of a Quadratic Equation
Sample Output
Value of a: 1

Value of b: -5

Value of c: 6

x1 = 3.000000
x2 = 2.000000


Process returned 0 (0x0)   execution time : 7.980 s
Press any key to continue.
Source Code
#include<stdio.h>
#include<math.h>
 
int main()
{
    int a, b, c;
    float x1, x2, root;
 
    printf("Value of a: ");
    scanf("%d", &a);
    printf("\nValue of b: ");
    scanf("%d", &b);
    printf("\nValue of c: ");
    scanf("%d", &c);
 
    root = sqrt(pow(b, 2) - (4 * a * c));
 
    x1 = (- b + root) / (2 * a);
    x2 = (- b - root) / (2 * a);
 
    printf("\nx1 = %f\nx2 = %f\n\n", x1, x2);
 
    return 0;
}
Sample Output
Value of a: 1

Value of b: -5

Value of c: 6

x1 = 3.000000
x2 = 2.000000


Process returned 0 (0x0)   execution time : 7.980 s
Press any key to continue.

No comments:

Post a Comment

Change Theme
X