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>

      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);

Recommended Answers

All 3 Replies

Try changing this line

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


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>

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

Member Avatar

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.