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.

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


qudratic eqn output
quadratic equation output

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