Program to solve Quadratic Equation.
Program:
--------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float x,x1,x2,D;
printf("enter the value of a:");
scanf("%d",&a);
printf("Enter the value of b:");
scanf("%d",&b);
printf("Enter the value of c:");
scanf("%d",&c);
D=sqrt(b*b)-(4*a*c);
if(D==0)
{
printf("roots are real and equal.");
x=((-b)/(2*a));
printf("\nx=%f",x);
}
else if(D>0)
{
printf("roots are real and distinct.");
x1=((-b+D)/(2*a));
x2=((-b-D)/(2*a));
printf("\nx1=%f",x1);
printf("\nx2=%f",x2);
}
else
{
printf("roots does not exist.");
}
getch();
}
----------------------------------------
Output :
==============================
Enter the value of a:1
Enter the value of b:2
Enter the value of c:1
roots does not exist.
============================
--------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float x,x1,x2,D;
printf("enter the value of a:");
scanf("%d",&a);
printf("Enter the value of b:");
scanf("%d",&b);
printf("Enter the value of c:");
scanf("%d",&c);
D=sqrt(b*b)-(4*a*c);
if(D==0)
{
printf("roots are real and equal.");
x=((-b)/(2*a));
printf("\nx=%f",x);
}
else if(D>0)
{
printf("roots are real and distinct.");
x1=((-b+D)/(2*a));
x2=((-b-D)/(2*a));
printf("\nx1=%f",x1);
printf("\nx2=%f",x2);
}
else
{
printf("roots does not exist.");
}
getch();
}
----------------------------------------
Output :
==============================
Enter the value of a:1
Enter the value of b:2
Enter the value of c:1
roots does not exist.
============================
Comments
Post a Comment