954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Quadratic equation program crashes

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();
}
Sommy
Newbie Poster
9 posts since Feb 2012
Reputation Points: 10
Solved Threads: 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>
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

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

Sommy
Newbie Poster
9 posts since Feb 2012
Reputation Points: 10
Solved Threads: 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);
MonsieurPointer
Junior Poster
125 posts since Jun 2011
Reputation Points: 31
Solved Threads: 12
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: