Can i get the following C syntax for the following problem please i need it in c
ID: 3767369 • Letter: C
Question
Can i get the following C syntax for the following problem please i need it in c not c++ thank you
Problem 1: Write a program to find the roots of a quadratic equation. Your program should evaluate all three possibilities, same roots, complex conjugate, and different roots. The user is supposed to input coefficients of the equations as real numbers. You can use library function sqrt() to find the square root of an argument (declare #include on the top. Use a sentinel loop to execute program few times. Sample output: Enter coefficients of the equation: Complex roots are 3 + j4 and 3 – j4
Explanation / Answer
Program:
Sample output:
Code to copy:
#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, d, r1,r2, rl, ig;
char choice;
//sentinal loop
while(1)
{
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
d=b*b-(4*a*c);//find the determinant
if (d>0) //if determinant is positive, the roots are different
{
r1= (-b+sqrt(d))/(2*a);
r2= (-b-sqrt(d))/(2*a);
printf("Roots are different and they are: %.2f and %.2f",r1 , r2);
}
else if (d==0)////if determinant is positive, the roots are same
{
r1 = r2 = -b/(2*a);
printf("Roots are same and they are: %.2f and %.2f", r1, r2);
}
else //if roots are complex conjugate
{
rl= -b/(2*a);//real value
ig = sqrt(-d)/(2*a);//imaginary value
printf("Roots are complex and they are: %.2f+%.2fj and %.2f-%.2fj ", rl, ig, rl, ig);
}
fflush(stdin);
//ask the user willing to continue
printf(" Do you want to find roots ?(y/n) ");
choice=getchar();
if(choice=='n')
break;
}
printf(" Thank you... ");
return 0;
}