Hello all,

I'm trying to write a program that will compute the real roots of the quadratic equation ax^2 + bx + c = 0 given by:

x1 = (- b + sqrt (b^2 - 4ac)) / 2a and x2 = (- b - sqrt (b^2 - 4ac)) / 2a.

Based on the values of "a", "b", and "c" entered by a user, the roots will be calculated based on the following set of rules:

1. if a and b are zero, there are no solutions.
2. if a is zero, then there in one real root (-c / b).
3. if the discriminate (sqrt(b^2 - 4ac)) is negative, then there are no real roots.
4. for all other combinations, there are two real roots.

I figured I could accomplish this with a series of "if" statements....no else statements....

The code I came up with is as follows:

int main()
{
	float a;
	float b;
	float c;
	float x1;
	float x2;
	float one_root;
	
	printf("Please enter value for a:");
	scanf("%f", &a);
	
	printf("Please enter value for b:");
	scanf("%f", &b);

	printf("Please enter value for c:");
	scanf("%f", &c);
	
	if (a == 0 && b ==0)
		{
			printf("There are no solutions.");
		}
	if (a == 0)
		{
			one root = -c / b;
			printf("There is one root of %f.);
		}
	if (sqrt(pow (b ,2)) - (4 * a * c)) < 0)
		{
			printf("There are no real roots.");
		}
	if (a < 0 && a > 0 && b < 0 && b > 0)
		{
			x1 = (-b + sqrt ((pow (b, 2)) - (4 * a * c))) / (2 * a);
			x2 = (-b + sqrt ((pow (b, 2)) - (4 * a * c))) / (2 * a);
			printf("The two real roots are %f and %f.", x1, x2);
		}

	return 0;
}

I haven't had a chance to try it to see if it works because I can't get to a computer with software that will let me do it right now. What I really want to know right now is that it's ok to write the code just using "if" statements and leaving out the "else".

Thanks

Dani AI

Generated

A few practical corrections and tips that go beyond the "else if" fix suggested.

First, check cases in a safe order and treat tiny floating values with an epsilon instead of == 0. If both a and b are effectively zero then either there are no solutions or infinitely many (when c is also zero). Do not take sqrt until you know the discriminant is non‑negative; compare the discriminant D = b*b - 4*a*c (do not call sqrt to test sign). Use double for better precision and prefer b*b instead of pow(b,2).

For numerical robustness when there are two distinct real roots, use a stable evaluation to avoid catastrophic cancellation (see the quadratic equation discussion). A concise, safe flow:

  • test a and b near zero (handle infinite/no-solution and linear cases),
  • compute D = b*b - 4*a*c,
  • if D < 0 → no real roots, if D == 0 → one real (double) root,
  • otherwise compute roots with a numerically stable formula.

Example C sketch (uses an epsilon, checks scanf, compiles with -lm):

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

/* Read a,b,c ; handle special cases, then compute roots robustly. */

Troubleshooting notes: check scanf return value, avoid dividing by zero (test b before computing -c/b), use fabs with a sensible EPS for zero comparisons, and print with %g for concise output. These changes fix the logical errors found in the original post and give correct, stable results for typical and extreme inputs.

References: Quadratic equation.

Recommended Answers

All 2 Replies

Use else if statement. According to your code, if a=0 and b=0 then both the first and the second if statements will be executed. And the output will be
"There are no solutions"
"There is one solution x"

use this

if (a == 0 && b ==0)
		{
			printf("There are no solutions.");
		}
       else if (a == 0)
		{
			one root = -c / b;
			printf("There is one root of %f.);
		}
	else if (sqrt(pow (b ,2)) - (4 * a * c)) < 0)
		{
			printf("There are no real roots.");
		}
	else if (a < 0 && a > 0 && b < 0 && b > 0)
		{
			x1 = (-b + sqrt ((pow (b, 2)) - (4 * a * c))) / (2 * a);
			x2 = (-b + sqrt ((pow (b, 2)) - (4 * a * c))) / (2 * a);
			printf("The two real roots are %f and %f.", x1, x2);
		}

Thanks, hammer! Seems to be working.

Be a part of the DaniWeb community

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