0

Hello...just started learning C and i'm trying to write a program that solves quadratic equations...but every time i compile and run it and enter the 3 variables it crashes. I kept on looking but didn't find anything wrong with it...help? :P

#include <stdio.h>

main(){
      int a,b,c;
      float x1,x2;
      printf("Please enter a b c: \n");
      scanf("%d %d %d",a,b,c);
      if (a == 1){
            x1= (b/2) + sqrt(pow(b/2,2)-c);
            x2= (b/2) - sqrt(pow(b/2,2)-c);
            } else {
            x1= (b+ sqrt(pow(b,2) - 4*a*c))/(2*a);
            x2= (b- sqrt(pow(b,2) - 4*a*c))/(2*a);
            }
      printf("The solutions are: \n%f ja %f",x1,x2);
      getch();
}
3
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by MonsieurPointer
0

Try changing this line

scanf("%d %d %d",a,b,c);

to

scanf("%d %d %d",&a,&b,&c);

A few more pointers..

main should return an integer

int main()
{
...
return 0;
}

and you should include the library math.h for the math function(s).

#include <stdio.h>
#include <math.h>

Edited by gerard4143: n/a

0

Ah yes...of course... :D Thank you, that helped =)

0

You should also check to see if a is 0, otherwise you will have a divide-by-zero issue in

x1= (b+ sqrt(pow(b,2) - 4*a*c))/(2*a);
x2= (b- sqrt(pow(b,2) - 4*a*c))/(2*a);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.