//Program to find roots of a quadratic equation (Ax^2 +Bx +C)
#include<stdio.h>
#include<math.h>
#include<conio.h>
main()
{
int a, b, c;
float d, root1, root2;
clrscr(); // for clearing the console screen
for(;;)
{
printf("\nEnter the non zero co-efficients A, B and C (Ax^2 +Bx +C): \n");
scanf("%d%d%d",&a,&b,&c);
if((a==0) || (b==0) || (c==0)) // case 1 : if no such equation exists
{
printf("\nPlease enter non zero co-efficients \n");
}
else // case 2 : if they exists
{
d=((b*b)-(4*a*c)); // calculating discriminant
if(d>0) // if the roots are real
{
printf("Roots are real! \n");
root1=(-b-(sqrt(d)))/(2.0*a);
root2=(-b+(sqrt(d)))/(2.0*a);
printf("Roots are: %f and %f \n", root1, root2);
}
else if(d<0) // if the roots are imaginary
{
printf("Roots are Imaginary:\n");
root1=-b/(2.0*a);
printf("First root: %lf +%fi \n", root1, sqrt(-d)/(2.0*a));
printf("Second root:%lf -%lfi\n", root1, sqrt(-d)/(2.0*a));
}
else // if the roots are equal
{
printf("Roots are equal!\n");
root1= -b/(2.0*a);
printf("Equal; roots are:%f and %f \n", root1,root1);
}
}
}
getch(); // for holding the console screen
return 0;
}
#include<stdio.h>
#include<math.h>
#include<conio.h>
main()
{
int a, b, c;
float d, root1, root2;
clrscr(); // for clearing the console screen
for(;;)
{
printf("\nEnter the non zero co-efficients A, B and C (Ax^2 +Bx +C): \n");
scanf("%d%d%d",&a,&b,&c);
if((a==0) || (b==0) || (c==0)) // case 1 : if no such equation exists
{
printf("\nPlease enter non zero co-efficients \n");
}
else // case 2 : if they exists
{
d=((b*b)-(4*a*c)); // calculating discriminant
if(d>0) // if the roots are real
{
printf("Roots are real! \n");
root1=(-b-(sqrt(d)))/(2.0*a);
root2=(-b+(sqrt(d)))/(2.0*a);
printf("Roots are: %f and %f \n", root1, root2);
}
else if(d<0) // if the roots are imaginary
{
printf("Roots are Imaginary:\n");
root1=-b/(2.0*a);
printf("First root: %lf +%fi \n", root1, sqrt(-d)/(2.0*a));
printf("Second root:%lf -%lfi\n", root1, sqrt(-d)/(2.0*a));
}
else // if the roots are equal
{
printf("Roots are equal!\n");
root1= -b/(2.0*a);
printf("Equal; roots are:%f and %f \n", root1,root1);
}
}
}
getch(); // for holding the console screen
return 0;
}
No comments:
Post a Comment