WAP in C to calculate the roots of quadratic equation and their nature using if else

WAP in C to calculate the roots of quadratic equation and their nature using if else

Write a program in C to calculate the roots of the quadratic equation and the nature of roots using if-else.

The nature of the roots of an equation is defined as it’s nature of containing real solutions or not.

In the case of quadratic equations,

Nature of roots of an equation ax2 + bx + c is derived by the following formula:-

x = −b ± √(b2 − 4ac)/2a

In the above expression, we have b2 – 4ac. It is discriminant because it can “discriminate” between the possible types of answer:

  1. If b2 – 4ac is greater than 0, then 2 real distinct roots exist.
  2. If this is equal to 0, then equal roots of the equation exist.
  3. And if this is less than 0, then the roots of the equation do not exist.
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main() {
  float a,b,c,root1,root2,real,imag,disc;
  printf("Enter the value of a,b and c:\n");
  scanf("%f%f%f",&a,&b,&c);
  disc=b*b-4*a*c;
  if(disc==0) {
    root1=-b/(2*a);
    printf("\nRoots are real and equal \nRoot1 = Root2 = %.3f",root1);
  }
  else if(disc>0) {
    root1=(-b+sqrt(disc))/(2*a);
    root2=(-b-sqrt(disc))/(2*a);
    printf("\nRoots are real and unequal \nRoot1 = %.3f\nRoot2 = %.3f",root1,root2);
  }
  else {
    real=-b/(2*a);
    imag=sqrt(-disc)/(2*a);
    printf("\nRoots are imaginary \nRoot1 = %.3f + i%.3f \nRoot2 = %.3f - i%.3f",real,imag,real,imag);
  }
  getch();
  return 0;
}

Output:

qudratic eqn output
quadratic equation output

Also read, how to check the year is a leap year or not.

Share this post