Write C code that does the following: 1 . Interactively read the three coefficie
ID: 3886129 • Letter: W
Question
Write C code that does the following:1. Interactively read the three coefficients of a quadratic equation and compute the two roots. The program must alert if there is no real root, i.e. read a, b, c of
and compute
(b ± b^2 4ac)/2a
Note: sqrt is available in math.h. You need to compile the program with "-lm" option, i.e.
Make sure that you add a routine to check the positiveness of the discriminant using the if statement. Show actual runs and examples.2. Interactively read temperature in Celsius and convert it to Fahrenheit. Note
Example:
a x2 + b x + c = 0,Explanation / Answer
#include <stdio.h>
int main(void) {
// your code goes here
float c,f;
printf("enter Temperature in C:");
scanf("%f",&c);
f=c*1.8+32;
printf(" It is %f degree in Fahreheit",f);
return 0;
}
Output:
enter Temperature in C:29
It is 84.199997 degree in Fahreheit
1)
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, determinant, root1,root2, realPart, imaginaryPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
determinant = b*b-4*a*c;
// condition for real and different roots
if (determinant > 0)
{
// sqrt() function returns square root
root1 = (-b+sqrt(determinant))/(2*a);
root2 = (-b-sqrt(determinant))/(2*a);
printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);
}
//condition for real and equal roots
else if (determinant == 0)
{
root1 = root2 = -b/(2*a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else
{
printf("Roots are imaginary");
}
return 0;
}
Output:
Enter coefficients a, b and c: 1 5 6
root1 = -2.00 and root2 = -3.00
Enter Coefficient a,b and c :2 4 8
Roots are Imaginery
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.