Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

help me solved this question using c++

0 votes
asked May 11, 2018 by Mahmud Muhammad (230 points)
write a program that calculate the root of quadratic ax2+bx+c=0 given the cooficient a,b,c use the quadratic formula, your program should solved for both real and complex roots.

1 Answer

0 votes
answered May 13, 2018 by ali
#include<iostream>

 #include<math.h>

  using namespace std;

main() {

 float a,b,c; double d; //because sqrt function accepts double data type

 cout<<"Enter the Coefficient 'a' : "; cin>>a;

while(a == 0) //if a=0, take the input again

{ cout<<"Enter a Non-Zero value for 'a' : "; cin>>a;

 }

 cout<<"Enter the coefficient 'b' : "; cin>>b;

cout<<"Enter the coefficient 'c' : "; cin>>c;

 d = b*b - 4*a*c; //computing the determinant

if(d == 0)

 {

 cout<<"\nRoots are Real and Equal"<<endl<<endl;

cout<<"Root 1 is "<<-b/(2*a)<<endl;

cout<<"Root 2 is "<<-b/(2*a)<<endl; }

 else if(d > 0) {

cout<<"\nRoots are Real and Different"<<endl<<endl;

 cout<<"Root 1 is "<< (-b + sqrt(d))/2*a<<endl;

 cout<<"Root 2 is "<< (-b - sqrt(d))/2*a<<endl; }

else {

d = (-1*d); //making determinant positive, because you cannot directly compute the square root of a negative number in c++

cout<<"\nRoots are Complex and Different"<<endl<<endl;

 cout<<"Root 1 is "<<-b/(2*a)<<" + i "<<sqrt(d)/2*a<<endl;

cout<<"Root 2 is "<<-b/(2*a)<<" - i "<<sqrt(d)/2*a<<endl; }

system("pause"); }}
commented May 14, 2018 by karna bc
Q..For this code snippet, identify the conditions under which errors occur/code fails to act as expected.
#define max 10
int main()
{
char a[max];
int total=1;
char ch;
for(int i=0;i
{
cin>>a[i];
total=total*a[i];
cout<<”Do you want to add?\n”;
cin>>ch;
if(ch==’n’)
break;
}
for(int i=0;i
cout<
cout<
return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...