Thursday, 26 January 2012

Write a C program to find the roots of a Quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int a , b, c, d;
    float x1, x2,ab;
    ab:
    printf("Enter the value of a, b, c:");
    scanf("%d%d%d", &a, &b, &c);
    if(a==0)
    {
        printf("\nIt is a Linear Equation");
        printf("\nRe-Enter the data again.");
        goto ab;
    }
    d=(b*b)-(4*a*c);
    if(d==0)
    {
        printf("\nRoots are real and equal");
        x1= x2=(-b)/(2*a);
        printf("\nRoots are: %f \t %f ", x1, x2);
    }
    else
    {
        if(d<0)
        printf("\nRoots are imaginary");
        else
        {
            x1=((-b)+sqrt(d))/(2*a);
            x2=((-b)-sqrt(d))/(2*a);
            printf("\nRoots are real");
            printf("\nRoots are : %f \t %f", x1, x2);
        }
    }
    getch( );
}


Output:-
Enter the values of a, b,c : 5
2
4
Roots are imaginary.

No comments:

Post a Comment