Solve quadratic equations in C

Friday, June 18, 2010

Today, I tried to translate the very basic "solve quadratic equations" program. I made it while i use C complier of Microsoft : Visual Studio 2005. I have used it since i was a first year student

// C08_Ex 4: To solve quadratic equation: ax^2 + bx + c = 0

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

void main()
{
    int a, b, c;
    printf("Input a, b, c: ");
    scanf("%d%d%d", &a, &b, &c);
  
    if (a == 0)
    {
        // To solve a simple equation: bx + c = 0
        if (b == 0)
        {
            if (c == 0)
                printf("=> Equation has multitudinous root\n");
            else
                printf("=> Equation has not root\n");
        }
        else
            printf("=> Equation only has a root x = %.2f\n", (float)-c/b);
    }
    else
    {
        double delta = b*b - 4*a*c;

        if (delta < 0)
            printf("=> Equation has not root\n");
        else
        {
            if (delta == 0)
                printf("=> Equation has : x1 = x2 = %.2f\n", (float)-b/(2*a));
            else
            {
                float x1 = (float)(-b - sqrt(delta))/(2*a);
                float x2 = (float)(-b + sqrt(delta))/(2*a);
                printf("=> Equation has 2 root x1 = %.2f, x2 = %.2f\n", x1, x2);
            }
        }
    }
}

solve quadratic equations in C
 solve quadratic equations in C

0 Comments:

Post a Comment

 
 
 
 
Copyright © 2010 C Source Code - C++ Source Code. All Rights Reserved. Using Xclear Theme | Bloggerized by Themescook Developed by Helios | Powered by Blogger