Home / C Programs / C program to find roots of quadratic equation

C program to find roots of quadratic equation

C program to find roots of quadratic equation : In , quadratic equation is second degree univariate polynomial equation. A general quadratic equation can be represented by :

C program to find roots of quadratic equation

where a, b and c are the coefficients of quadratic equation and a ≠ 0 . If a = 0 then it will be linear .

Now the solutions of quadratic equation can have distinct roots, equal roots or complex roots depending upon the value of discriminant.

Discriminant of a quadratic equation is an constant value equal to b^2 -4ac. Now,

If Discriminant>0, then quadratic equation will have distinct roots.

If Discriminant=0, then Quadratic equation will have equal roots.

If Discriminant<0, then Quadratic equation will have complex roots.

I have written a C program to find roots of quadratic equation which accepts coefficients of quadratic equation as input and  calculates and displays the roots of quadratic equation.

Platform : C Language

Compiler : Dev C++

C program to find roots of quadratic equation :

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

int main()
{
float a,b,c,x1,x2,disc;
printf("-------------------------------------------------------------\n");
printf("----------------made by C code champ ------------------------\n");
printf("-------------------------------------------------------------\n");
printf("\n  C Program for Calcualting roots of Quadratic Equation\n\n"); 
printf("Enter coefficient of X^2 : ");
scanf("%f",&a);
printf("\nEnter coefficient of X^1 : ");
scanf("%f",&b);
printf("\nEnter coefficient of X^0 : ");
scanf("%f",&c);
/*Finding discriminant*/
disc=b*b-4*a*c;
/*distinct roots*/
if(disc>0)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("\nThe roots are distinct\n");
printf("x1=%f\nx2=%f\n",x1,x2);
getch();
exit(0);
}
/*Equal roots*/
if(disc==0)
{
x1=x2=-b/(2*a);
printf("\nThe roots are equal\n");
printf("x1=%f\nx2=%f\n",x1,x2);
getch();
exit(0);
}
/*complex roots*/
x1=-b/(2*a);
x2=sqrt(fabs(disc))/(2*a);
printf("\nThe roots are complex.\n");
printf("The first root=%f+i%f\n",x1,x2);
printf("The second root=%f-i%f\n",x1,x2);
getch();
}

We hope you all have enjoyed the C program to find roots of quadratic equation. If you have any issues or concerns with the logic ask us in form of comments.

About Mr Coder

Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games. Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com

Check Also

C Program to Implement Stack

This C Program implements stack. Stack is an area of memory that holds all local …

7 comments

  1. A wonrdeful job. Super helpful information.

  2. I every time emailed this website post page to all my contacts, as if like to read it afterward my links will too.

  3. Nice post. I learn something new and challenging on websites I stumbleupon everyday. It will always be exciting to read articles from other writers and use something from their websites.

  4. just beneath, are numerous totally not related sites to ours, however, they are surely worth going over.

  5. plz explain the code
    x2=sqrt(fabs(disc))/(2*a);
    what is “fabs” meaning?

  6. Fabs function is function absolute value function defined under math.h. It is used to calculate absolute value of arg.
    Say num = -1234.00 then its absolute value will be 1234 i.e. fabs(num) =1234.

  7. pls tell me c program

    *
    * *
    * * *
    * * * *
    * * * * *
    * * * *
    * * *
    * *
    *

Leave a Reply

Your email address will not be published. Required fields are marked *