C program: quadratic equation

Solved
berlingo-48 Posted messages 45 Status Membre -  
 Cool -
Hello,

I just created a C program that processes a quadratic equation, and I need a correction please.

The program is:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

double a=0,b=0,c=0, delta=0;
double rst1=0, rst2=0, rst3=0 ;
printf("Enter a value for a: ");
scanf("%lf", &a);
printf("Enter a value for b: ");
scanf("%lf", &b);
printf("Enter a value for c: ");
scanf("%lf", &c);
if (a==0){
if (b==0) {
if (c==0) {
printf("any real number is a solution") ;}
else {
printf("there is no solution"); }
}
else {
rst1 = -c/b ;
printf("the solution is %lf", rst1);}
}
else {
delta = b*b - 4*a*c ;
if (delta < 0) {
printf("there is no solution in the reals"}
else {
rst2 = (-b + sqrt(delta))/ (2*a) ;
rst3 = (-b - sqrt(delta))/ (2*a) ;
printf("there are two solutions equal to %lf and %lf", rst2, rst3) ; }

system("PAUSE");
return 0;
}

Thank you in advance

Configuration: Windows 7 / Chrome 13.0.782.218

3 réponses

matooo
 
Here, I offer you a simpler solution!
#include <stdio.h>
#include <stdlib.h>

int main(void)

{
float x1, x2, delta, a, b, c;
printf("Enter the values of: a, b, c \n");
scanf("%f %f %f",&a, &b, &c);

delta = (b*b)-(4*a*c);

if (delta == 0.0)
{
x1 = -b /(2*a);
printf("the unique solution is xs = %.2f \n",x1);
}
if (delta > 0.0)
{
x1 = (-b - sqrt(delta))/(2*a);
x2 = (-b + sqrt(delta))/(2*a);
printf("the two roots are: x1 = %.2f and x2 = %.2f \n",x1, x2);
}
if (delta < 0.0)

printf("the equation has no solution");

return 0;
}
90
fiddy Posted messages 441 Registration date   Status Contributeur Last intervention   1 847
 
Simpler but less comprehensive...
3
matcho
 
il faut #include pour que sqrt fonctionne
2