Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Help with conditional statements on C programming  (Read 7225 times)

0 Members and 1 Guest are viewing this topic.

10ali01

  • Guest
Help with conditional statements on C programming
« 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;
}

vishalsaxena17



    Beginner
    Re: Help with conditional statements on C programming
    « 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;

    }
    I never loose , always learn from mistakes.....

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Help with conditional statements on C programming
    « 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 »
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein