Write a program to solve quadratic equation



Write a program to solve quadratic equation

 
// Write a program to solve quadratic equation          
// Ex: 1x^2+2x+1 = 0 -> x=-1 and x=-1

 
#include
#include
int main(){
    int a,b,c;
    printf("A quadratic equation will be something like: ax^2+bx+c=0\n");              // input from user
    printf("Enter a:");
    scanf("%d",&a);
    printf("Enter b:");
    scanf("%d",&b);
    printf("Enter c:");
    scanf("%d",&c);
    int delta = (int)pow(b,2)-4*a*c;
    printf("Equation: %dx^2+%dx+%d=0\n",a,b,c);
    if(delta<0){
        printf("No solution for this equation\n");
    }
    else{
        double alpha = (-b+sqrt(delta))/(2*a);
        double beta  = (-b-sqrt(delta))/(2*a);
        printf("Solutions: x=%.2f and x=%.2f",alpha,beta);
    }  
    getch();
}

No comments:

Post a Comment