--S 31nx-Wolfram! R Advanced Hom AdvancedHW4: full book.pdf | zy Home-zyBookl zy
ID: 3731303 • Letter: #
Question
--S 31nx-Wolfram! R Advanced Hom AdvancedHW4: full book.pdf | zy Home-zyBookl zy 5.14. Lab:C × Chegg Study Ic e- Advan. + v https://learn R106Winter2018/chapter/5/section/14 zyBooks My library> EGR 106 home> 5.14: Lab: Quadratic Equation Solver ezyBooks catalog Help/FAQ Alexander Kuzner 5.14 Lab: Quadratic Equation Solver Write a program that determines whether a quadratic equation has one real root, two real roots, or imaginary roots. Then, if the roots are real, display the roots of the quadratic equation. A quadratic equation is of the form ax"2+bx+c=0 The discriminant of the equation is D-bA2-4ac. If D>0, then there are two real roots. If D-0, then there is one real root, and if DExplanation / Answer
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float d,root1,root2;
int loop;
do{
printf("Enter a value for a:");
scanf("%f",&a);
printf("Enter a value for b:");
scanf("%f",&b);
printf("Enter a value for c:");
scanf("%f",&c);
printf(" ");
printf("You entered Equation %.0fx^2+%0.fx+%0.f ",a,b,c);
printf(" ");
d = b * b - 4 * a * c;
if(d < 0){
printf("Equation has imaginary roots. ");
printf("X=%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
printf("or X= %.3f%+.3fi ",-b/(2*a),-sqrt(-d)/(2*a));
}
else if(d==0){
printf("Equation has two identical real roots. ");
root1 = -b /(2* a);
printf("X= %.3f ",root1);
}
else{
printf("Equation has two distinct real roots. ");
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
printf("X= %.3f or X= %.3f ",root1,root2);
}
printf(" Another Equation? (Enter 0 to exit)...1");
scanf("%d",&loop);
}while(loop);
return 0;
}
Hi,
The code instructions are given in the program. Each line by line and the program given above has implemented reading the instructions
commend code is below please refer in case you have any doubts in the program
The instruction to implement code is clear and each step has been given in program description itself. Please go throught the comments if don't understand something
Commented code below
#include<stdio.h>
#include<math.h>
//main function started
int main(){
//declaring 3 variables for quadratic coefficients a, b and c
float a,b,c;
float d,root1,root2;
int loop;
// now we need to run the program until the user enters zero using loop variable as do-while condition's break value
do{
//scanning the input from user for a, b and c and storing them respectively
printf("Enter a value for a:");
scanf("%f",&a);
printf("Enter a value for b:");
scanf("%f",&b);
printf("Enter a value for c:");
scanf("%f",&c);
printf(" ");
printf("You entered Equation %.0fx^2+%0.fx+%0.f ",a,b,c);
printf(" ");
// calculating d so we will know what kind of roots equation will have
d = b * b - 4 * a * c;
//all logic is down below to calculate roots given according to the formulas
if(d < 0){
printf("Equation has imaginary roots. ");
printf("X=%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
printf("or X= %.3f%+.3fi ",-b/(2*a),-sqrt(-d)/(2*a));
}
else if(d==0){
printf("Equation has two identical real roots. ");
root1 = -b /(2* a);
printf("X= %.3f ",root1);
}
else{
printf("Equation has two distinct real roots. ");
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
printf("X= %.3f or X= %.3f ",root1,root2);
}
printf(" Another Equation? (Enter 0 to exit)...1");
scanf("%d",&loop);
}while(loop);
return 0;
}
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float d,root1,root2;
int loop;
do{
printf("Enter a value for a:");
scanf("%f",&a);
printf("Enter a value for b:");
scanf("%f",&b);
printf("Enter a value for c:");
scanf("%f",&c);
printf(" ");
printf("You entered Equation %.0fx^2+%0.fx+%0.f ",a,b,c);
printf(" ");
d = b * b - 4 * a * c;
if(d < 0){
printf("Equation has imaginary roots. ");
printf("X=%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
printf("or X= %.3f%+.3fi ",-b/(2*a),-sqrt(-d)/(2*a));
}
else if(d==0){
printf("Equation has two identical real roots. ");
root1 = -b /(2* a);
printf("X= %.3f ",root1);
}
else{
printf("Equation has two distinct real roots. ");
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
printf("X= %.3f or X= %.3f ",root1,root2);
}
printf(" Another Equation? (Enter 0 to exit)...1");
scanf("%d",&loop);
}while(loop);
return 0;
}
gcc version 4.6.3 Enter a value for a: -2 Enter a value for b: 7 Enter a value for c: 4 You entered Equation -2x^2+7x+4 Equation has two distinct real roots. X= -0.500 or X= 4.000 Another Equation? (Enter 0 to exit)...1 1 Enter a value for a: 2 Enter a value for b: 8 Enter a value for c: 8 You entered Equation 2x^2+8x+8 Equation has two identical real roots. X= -2.000 Another Equation? (Enter 0 to exit)...1 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.