I just wanna know how I can optimize my C code. My program works fine, I tested it with many different values, all is good. However, I'd like to reduce the number of lines and write my program in better quality. Here's the source code:
#include <stdio.h>
#include <math.h>
int main(void) {
float a,b,c,x,x1,x2;
printf("aX^2 + bX + c = 0\n");
printf("Type the value of a: ");
scanf("%f", &a);
printf("Type the value of b: ");
scanf("%f", &b);
printf("Type the value of c: ");
scanf("%f", &c);
if ( a!=0 && b!=0 && c!=0){
float delta = b*b - 4*a*c;
if (delta>0){
x1 = (-b-sqrt(delta))/(2*a);
x2 = (-b+sqrt(delta))/(2*a);
printf("Solutions are x1 = %f and x2 = %f\n",x1,x2);
}
else if (delta == 0){
x = -b/(2*a);
printf("One unique solution is x = %f\n", x);
}
else {
printf("No solutions !\n");
}
}
if ( a==0 && b!=0 && c!=0)
printf("One unique solution x = %f\n", -c/b);
if ( a==0 && b==0 && c!=0)
printf("No solutions !\n");
if ( a==0 && b==0 && c==0 )
printf("Set of solutions is R\n");
if (a!=0 && b==0 & c!=0) {
x = -c/a;
if(x>=0)
printf("Two soltions x = %f et -x = %f\n", sqrt(x),-sqrt(x));
else{
printf("No solutions !\n");
}
}
if (a!=0 && b==0 && c==0)
printf("One unique x = 0\n");
}