1.

Solve : Help with conditional statements on C programming?

Answer»

I am a BEGINNER on C programming and an error message (underlined) that I have typed on the below program is not working....can anyone help please???
I am solving a quadratic equation the -b +/- root(b*b-4ac)/2a. If b*b is less than 4ac then there are no real roots so I need an error message. So i have made the bracket (f) and said that if it is less than zero the error message should come up....but its not! Any help would be grateful!!!



#include
#include
int MAIN() {
FLOAT a, b ,c, d, e, f, root, x1,x2, b2;
printf("Enter a: ");
scanf("%f",&a);
printf("Enter b: ");
scanf("%f",&b);
printf("Enter c: ");
scanf("%f",&c);
b2 = pow(b,2);
d = (4*a*c);
f = (b2-d);

if(f<0) {
printf("Error - no real roots!\n"); /*Error messaging*/
return 0;
}
root = sqrt(f);
e = root/(2*a);
x1 = ((-b)+e);
x2 = ((-b)-e);
printf("Solution=%f\n",x1);
printf("Solution=%f\n",x2);
return 0;
}
wel i understand your problem , the small thing that is missing in your code is
" #INCLUDE header FILE because of which you are unable to calculate..
1>. " sqrt " function.

2>. " Pow " function so are getting unexpected RESULTS..

i am pasting the correct code , just check it out

#include
#include
#include
int main()
{
float a,b,c,d,e,f,root,x1,x2,b2;
printf("enter a: ");
scanf("%f",&a);
printf("enter b :");
scanf("%f",&b);
printf("enter c: ");
scanf(" %f",&c);
b2=b*b;
d=(4*a*c);
f=(b2-d);
printf("the value of F is = %f",f);
if(f<0)
{
printf(" error - no real root") ;
return 0;
}

root = sqrt(f);
e=root/(2*a);
x1=((-b)+e);
x2=((-b)-e);
printf("solution =%f\n",x1);
printf("solution =%f\n",x2);
return 0;

}I know nothing about C. That said, I used the Tiny C Compiler and your code works just dandy.

Results:
Quote

C:\Temp\tcc>test
Enter a: 12
Enter b: 1
Enter c: 3
Error - no real roots!

Quote
C:\Temp\tcc>test
Enter a: 1
Enter b: 5
Enter c: 2
Solution=-2.938447
Solution=-7.061553

Seems fine to me.

PS. You did too include the math header file.


Discussion

No Comment Found