1. ## Quick Question

This is for a class. I am NOT asking for someone to do this for me. I just can't find out an issue and would appreciate it if anyone could point out its cause.

This is the program.
Code:
```int main(void)
{
//Variable declaration

double a, b, c, discriminant, r1, r2;

//Display title prompt
printf("This program finds the roots of the quadratic equation.\n");

//Prompt user for coefficients of the quadratic equation
printf("Input the a, b, and c coefficients of the quadratic equation\n in this form: (a)x^2 + (b)x + (c) \n");

//Read user values and assign to appropriate variables
scanf("%dx^2+%dx+%d", &a, &b, &c);

//Find root type
discriminant = (b*b-4*a*c);

//Find roots using the variables and display

if (discriminant>0)
{
r1 = -b+sqrt(discriminant)/(2*a);
r2 = +b+sqrt(discriminant)/(2*a);
printf("The two roots are real: ");
printf("%d %d\n");
}
else if (discriminant==0)
{
r1 = -b+sqrt(discriminant)/(2*a);
r2 = +b+sqrt(discriminant)/(2*a);
printf("The two roots are equal: ");
printf("%d %d\n");
}
else
{
r1 = -b+sqrt(discriminant)/(2*a);
r2 = +b+sqrt(discriminant)/(2*a);
printf("The two roots are complex and may not be correct: ");
printf("%d %d\n");
}

//Termination statements
system ("PAUSE");
return 0;
}```
My problem is that, no matter the variables, I always get the last else statement as the answer "The roots are complex...." even when I KNOW they're real or equal. Can't see why. Any help? 2. Any guesses? I've only just started learning, so if it's a simple error, don't hold it against me. 3. You're using the format for integers in scanf. The format for double is lf (that's a lowercase L).
Code:
`scanf("%lfx^2+%lfx+%lf", &a, &b, &c);` 4. I fixed that. It just made the wrong answer longer. Changed everything to float and put %f everywhere.

Any other ideas? EDIT:
You are not only using the wrong format in your printf's, you also don't have any variables listed!

So this
Code:
`		printf("%d %d\n");`
should be
Code:
`		printf("%f %f\n", r1, r2);` 6. Also changed discriminant to d. and some other syntax things that doesn't matter.
Current code:
Code:
```int main(void)
{
//Variable declaration

float a, b, c, d, r1, r2;

//Display title prompt
printf("This program finds the roots of the quadratic equation.\n");

//Prompt user for coefficients of the quadratic equation
printf("Input the a, b, and c coefficients of the quadratic equation\n in this form: (a)x^2 + (b)x + (c) \n");

//Read user values and assign to appropriate variables
scanf("%fx^2+%fx+%f", &a, &b, &c);

//Find root type
d = (b*b)-(4*a*c);

//Find roots using the variables and display

if (d>0)
{
r1 = (-b+(sqrt(d)))/(2*a);
r2 = (b+(sqrt(d)))/(2*a);
printf("The two roots are real: ");
printf("%f %f\n");
}
else if (d==0)
{
r1 = (-b+(sqrt(d)))/(2*a);
r2 = (b+(sqrt(d)))/(2*a);
printf("The two roots are equal: ");
printf("%f %f\n");
}
else
{
r1 = (-b+(sqrt(d)))/(2*a);
r2 = (b+(sqrt(d)))/(2*a);
printf("The two roots are complex and may not be correct: ");
printf("%f %f\n");
}

//Termination statements
system ("PAUSE");
return 0;
}``` 7. Originally Posted by oogabooga EDIT:
You are not only using the wrong format in your printf's, you also don't have any variables listed!

So this
Code:
`        printf("%d %d\n");`
should be
Code:
`        printf("%f %f\n", r1, r2);`
Oh..... lemme try fixing that. 8. Still wrong.

Code:
`//code deleted` 9. I figured out what I needed.

Thank you, oog. Appreciated.  Popular pages Recent additions 