Ques:-24) Write a program to compute the real roots of a quadratic equation

ax2 +bx +c =0

The roots are given by the equations

x1= -b + (sqrt(b2-4ac)/2a)

x2= -b – (sqrt(b2-4ac)/2a)

The program should request for the values of the constants a, b and c and print the values of x1 and x2. Use the following rules:

(a)    No solution, if both a and b are zero

(b)   There is only one root, if a=0 (x=-c/b)

(c)    There are no real roots, if b2-4ac is negative

(d)   Otherwise, there are two real roots

Test your program with appropriate data so that all logical paths are working as per your design. Incorporate appropriate output message.

				
					#include"stdio.h"
#include"conio.h"
void main()
{
   int a,b,c,x1,x2;
   clrscr();
   printf("------------real root of a quadratic equation--------------------\n")
   printf("enter the values of constants a,b and c:\n");
   scanf("%d%d%d",&a,&b,&c);
   x1=-b+(sqrt(pow(b,2)-4*a*c)/2*a);
   x2=-b-(sqrt(pow(b,2)-4*a*c)/2*a);

   if(a=b==0)
   {
      printf("No solution");
   }
   else if(a==0)
   {
     x1=(-c/b);
     printf("it has only one root i.e %d",x1);
   }
   else if((pow(b,2)-4*a*c)<0)
   {
     printf("Root is not real");
   }
   else
   {
     printf("the two roots are: %d and %d",x1,x2);
   }
   getch();
}

				
			

Out Put

				
					Enter ther value of constants a,b and c:
1 2 3 
it has ony one root i.e -1
				
			

Another Example

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

int main() {
    double a, b, c;
    double discriminant, x1, x2;

    // Requesting input from the user
    printf("Enter the values of a, b, and c: ");
    scanf("%lf %lf %lf", &a, &b, &c);

    // No solution if both a and b are zero
    if (a == 0 && b == 0) {
        printf("No solution, since both a and b are zero.\n");
        return 0;
    }

    // Only one root if a is zero
    if (a == 0) {
        x1 = -c / b;
        printf("There is only one root: x = %.2lf\n", x1);
        return 0;
    }

    // Calculate the discriminant
    discriminant = b * b - 4 * a * c;

    // No real roots if discriminant is negative
    if (discriminant < 0) {
        printf("There are no real roots, since the discriminant is negative.\n");
        return 0;
    }

    // Calculate the two real roots
    x1 = (-b + sqrt(discriminant)) / (2 * a);
    x2 = (-b - sqrt(discriminant)) / (2 * a);

    // Output the roots
    printf("The roots of the quadratic equation are: x1 = %.2lf and x2 = %.2lf\n", x1, x2);

    return 0;
}

				
			

Explanation:

  1. Input Request: The program starts by requesting the values of , , and from the user.
  2. No Solution: If both and are zero, it prints a message indicating that there is no solution.
  3. One Root: If is zero, it calculates and prints the single root =−/.
  4. Discriminant Calculation: It calculates the discriminant Δ=2−4.
  5. No Real Roots: If the discriminant is negative, it prints a message indicating that there are no real roots.
  6. Two Real Roots: If the discriminant is non-negative, it calculates the two roots using the quadratic formula and prints them.

Output:

Here are examples of the program’s output for different inputs:

  1. Both and are zero:

				
					Enter the values of a, b, and c: 0 0 5
No solution, since both a and b are zero.

				
			

Only one root:

				
					Enter the values of a, b, and c: 0 4 8
There is only one root: x = -2.00


				
			

No real roots:

				
					Enter the values of a, b, and c: 1 2 5
There are no real roots, since the discriminant is negative.


				
			

Two real roots:

				
					Enter the values of a, b, and c: 1 -3 2
The roots of the quadratic equation are: x1 = 2.00 and x2 = 1.00



				
			

To compile and run the program, you can use a C compiler like gcc:

				
					gcc program.c -o program
./program



				
			

Replace program.c with the filename of your C program.

Scroll to Top