13. Program that will take three values a, b and c and print the roots, if real, of the quadratic equation ax^2 + bx + c = 0.

Chapter 5: 

Conditional Structures

Programming Exercise

Problem # 13:

Write a program that will take three values a, b and c and print the roots, if real, of the quadratic equation ax^2 + bx + c = 0. Sample input 1: (a=1, b=1, c=-6 the output should be "Roots of the equation and 2 and -3"). Sample input 2 (if the input is a=1, b=0, c=9, the output should be "Sorry the roots are not real.")

Solution:

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main ()
{
    float a,b,c;
    double x,y;
    cout<<"Enter three values:";
    cin>>a>>b>>c;
    if(b*b-4*a*c>=0)
    {
        x=(-b+sqrt(b*b-4*a*c))/2*a;
        y=(-b-sqrt(b*b-4*a*c))/2*a;
        cout<<"\nRoots of the equation are "<<x<<" and "<<y;
    }
    else
        cout<<"\nSorry the roots are not real";
    return 0;
}


Let me know in the comment section if you have any question.

Previous Post:
12. Program that converts MILITARY time to STANDARD time.

Comments