C program to find the roots of an equation ax2 + bx + c = 0


/* C program to find the roots of an equation ax2 + bx + c = 0. */


#include"stdio.h"
#include"math.h"

void main()
{
float a, b, c, delta, r1, r2, x, i, sr;
printf("Enter a:");
scanf("%f", &a);
printf("Enter b:");
scanf("%f", &b);
printf("Enter c:");
scanf("%f", &c);
printf("Enter i:");
scanf("%f", i);
delta = pow(b, 2) - 4*a*c;
sr = sqrt(delta);
printf("Delta is: %.2f", delta);

if(delta==0)
{
r1 = r2 = (-b/ (2*a));
printf("r1 = r2 = %.2f", r2);
}

else if(delta > 0)
{
r1 = (-b + sr)/ 2*a;
r2 = (-b - sr)/ 2*a;
printf("The roots of the equation are : %f %f", r1, r2);
}

else
{
r1 = (-b / 2*a) + (i*(-sr/2*a));
r2 = (-b / 2*a) - (i*(-sr/2*a));
printf("The roots of the equation are: %f %f", r1, r2);
}

}

Comments

Popular Posts