Home / Software / Computer programming / Help with conditional statements on C programming
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: Help with conditional statements on C programming  (Read 2082 times)
10ali01
Topic Starter
Newbie



Posts: 1


« on: April 16, 2008, 08:40:04 AM »

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 <stdio.h>
#include <math.h>
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;
}
IP logged
vishalsaxena17
Beginner



Posts: 78




« Reply #1 on: April 18, 2008, 04:18:27 AM »

wel i understand your problem , the small thing that is missing in your code is
" #INCLUDE <MATH.H>  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<stdio.h>
#include<conio.h>
#include<math.h>
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;

}
IP logged

I never loose , always learn from mistakes.....
Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #2 on: April 19, 2008, 03:47:39 PM »

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. :P
« Last Edit: April 19, 2008, 04:49:18 PM by Sidewinder » IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
Pages: [1] - (Top) Print 
Home / Software / Computer programming / Help with conditional statements on C programming « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.112 seconds with 20 queries.