C program to find roots of a quadratic equation

C program to find roots of a quadratic equation


C program to find roots of a quadratic equation. It calculates the roots of a quadratic equation. 

Coefficients are assumed to be integers, but roots may or may not be real. For a quadratic equation ax2+ bx + c = 0 (a≠0), discriminant (b*b-4*a*c) decides the nature of roots.

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

void main()
{
int a,b,c,d;
double root1,root2;

printf("enter a,b,c  where  a*x*x+b*x+c=0\n");
scanf("%d%d%d",&a,&b,&c);

d=b*b-4*a*c;

if(d<0)   //for complex roots
{
printf("First root=%.2lf+i%.2lf\n ", -b/(double)(2*a), sqrt(-d)/(2*a));
printf("Second root=%.2lf-i%.2lf\n",-b/(double)(2*a),sqrt(-d)/(2*a));
}

else  //real root
{
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);

printf("First root=%.2lf\n",root1);
printf("Second root=%.2lf\n",root2);
}

getch();
}


Output:-
















No comments:

Post a Comment