Calculate the real roots of a quadratic equation

Click For Summary
SUMMARY

The discussion focuses on a C program designed to calculate the real roots of a quadratic equation. The primary issue identified is the incorrect use of the format specifier in the scanf function, where %f should be replaced with %lf for double variables. Additionally, the else statement requires braces to properly encapsulate its body. These corrections ensure the program compiles and executes correctly to compute the roots.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with quadratic equations and their mathematical properties
  • Knowledge of the C standard library functions, particularly scanf and printf
  • Basic understanding of conditional statements in C
NEXT STEPS
  • Review the correct usage of format specifiers in C, specifically for scanf and printf
  • Learn about control flow in C, focusing on the proper use of braces in conditional statements
  • Explore error handling in C programs to manage user input effectively
  • Study the mathematical derivation of quadratic roots and how to implement it programmatically
USEFUL FOR

Students learning C programming, software developers working with mathematical computations, and anyone interested in understanding quadratic equations and their implementation in code.

beanryu
Messages
90
Reaction score
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
1. To read doubles, use %lf in scanf().
2. You will need braces around the else body.
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
14
Views
2K