Calculate the real roots of a quadratic equation

In summary, the program is designed to calculate the real roots of a quadratic equation, but there is an error in the code when using the "if" statement and reading doubles. The program will also need braces around the else statement.
  • #1
beanryu
92
0
Please Help!

I designed this "PROGRAM"! TO calculate the real roots of a quadratic equation...

but the compiler miracle C kept saying there's something wrong around the "if" word... saying "unrecognised types in comparison"
it seem SO FINE to me... what is wrong?!:eek:

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

int main(void)
{
/* Declare variables. */
double a,b,c,d,e,f;

printf("This program computes the real roots of a quadratic equations.\n");
printf("(recall that the general form of quadratic equation is ax^2+bx+c where a, b and c are canstants)\n");

printf("please enter the first coefficient.\n");
scanf("%f", &a);
printf("%f\n",a);

printf("please enter the second coefficient.\n");
scanf("%f", &b);
printf("%f\n",b);

printf("please enter the third coefficient.\n");
scanf("%f", &c);
printf("%f\n",c);

f=(b*b-4*a*c);

if(f<0)
printf("roots are not real.\n");

else
d=((-1)*b+sqrt(f))/(2*a);
e=((-1)*b-sqrt(f))/(2*a);
printf("%f, %f",d,e);

/* Exit program. */
return 0;
}
/*--------------------------------------------------*/
 
Last edited:
Physics news on Phys.org
  • #2
1. To read doubles, use %lf in scanf().
2. You will need braces around the else body.
 
  • #3


Dear user,

I can see that you have designed a program to calculate the real roots of a quadratic equation. However, you are getting an error message from the compiler stating that there is something wrong around the "if" statement. The error message specifically says "unrecognised types in comparison".

Upon reviewing your code, I noticed that you have declared the variables a, b, c, d, and e as double, but you are using the "%f" format specifier in the scanf() function, which is used for float data types. To fix this error, you can either change the data type of your variables to float or use the "%lf" format specifier in the scanf() function for double data types.

I hope this helps you fix the error and successfully calculate the real roots of a quadratic equation. Keep up the good work!
 

FAQ: Calculate the real roots of a quadratic equation

How do you calculate the real roots of a quadratic equation?

To calculate the real roots of a quadratic equation, you can use the quadratic formula. This formula is (-b±√(b^2-4ac))/2a, where a, b, and c are the coefficients of the quadratic equation in the form of ax^2 + bx + c = 0. Plug in the values for a, b, and c, and solve for x using the formula. The resulting values for x will be the real roots of the quadratic equation.

What if the quadratic equation has imaginary roots?

If the quadratic equation has imaginary roots, it means that the values of x that satisfy the equation are not real numbers. This can happen if the value inside the square root in the quadratic formula is negative. In this case, the equation has no real solutions and the roots are considered to be imaginary or complex numbers.

Can you use the quadratic formula for all quadratic equations?

Yes, the quadratic formula can be used to solve any quadratic equation in the form of ax^2 + bx + c = 0. It is a universal formula that can give the solutions for all quadratic equations, whether they have real or imaginary roots.

What is the difference between real and imaginary roots?

Real roots are solutions to a quadratic equation that are real numbers, meaning they can be plotted on a number line. Imaginary roots are solutions that involve the square root of a negative number and cannot be plotted on a number line. They are typically represented in the form of a+bi, where a and b are real numbers and i is the imaginary unit (√-1).

Can you have more than two real roots for a quadratic equation?

No, a quadratic equation can only have two real roots at most. This is because a quadratic equation is a polynomial of degree 2, meaning the highest exponent is 2. And according to the Fundamental Theorem of Algebra, any polynomial of degree n has at most n solutions. In the case of a quadratic equation, n is 2, so it can have at most 2 real solutions.

Similar threads

Replies
3
Views
1K
Replies
1
Views
9K
Replies
3
Views
910
Replies
12
Views
1K
Replies
2
Views
1K
Replies
21
Views
2K
Replies
9
Views
4K
Back
Top