Write a program to calculate the real roots of a quadratic equation ax2 + bc + c
ID: 3769328 • Letter: W
Question
Write a program to calculate the real roots of a quadratic equation ax2 + bc + c =0. First, prompt the user to enter the double precision coefficients a, b, c. Then call the function named check_roots to check if the quadratic equation has real roots or complex roots. If the roots are complex, print out an error message "Complex roots!" If the roots are real, print out the roots. The function check_roots should accept three double precision inputs, and return an integer type value indicating if the quadratic equation has real roots pr complex roots. Write a program to calculate the real roots of a quadratic equation ax2 + bc + c =0. First, prompt the user to enter the double precision coefficients a, b, c. Then call the function named check_roots to check if the quadratic equation has real roots or complex roots. If the roots are complex, print out an error message "Complex roots!" If the roots are real, print out the roots. The function check_roots should accept three double precision inputs, and return an integer type value indicating if the quadratic equation has real roots pr complex roots.Explanation / Answer
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <math.h> /* This is needed to use sqrt() function.*/
int check_root(double, double,double);
void calculate_root(double, double,double);
int main()
{
double a, b, c;
int m;
printf("Enter coefficients a, b and c: ");
scanf("%lf%lf%lf",&a,&b,&c);
m = check_root(a,b,c);
if(m==1){
calculate_root(a,b,c);}
else{
printf("complex roots!");}
getch();
return 0;
}
int check_root(double a,double b,double c)
{
float determinant=b*b-4*a*c;
if (determinant>0)
{
return 1;
}
else
{return 0;
}}
void calculate_root(double a, double b,double c){
float D,r1,r2;
D=(b*b-4*a*c);
r1=((-b+pow(D,0.5))/2*a);
r2=((-b-pow(D,0.5))/2*a);
printf("The roots are %lf %lf",r1,r2);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.