Write a program to calculate the roots of a given quadratic equation -
a(x^2) + bx + c = 0
Print roots specifying their nature. If roots are imaginary,
no need to print the roots.
Print the nature of roots in the form of an integer -
0 : if roots are real & same
1 : if roots are real & different
-1 : if roots are imaginary
Round off the roots and then print the integral part only i.e. without any decimal places.
You can assume that, input will always be a quadratic equation.
Input format :
a b c (separated by space)
Output format :
Line 1 : Nature of roots (0 or 1 or -1)
Line 2 : Root 1 and Root 2 (separated by space)
Sample Input 1 :
1 4 2
Sample Output 1 :
1
-1 -3
CODE :
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
int disc = (b*b) - (4*a*c);
if(disc<0)
{
cout << -1;
}
else
{
float x = sqrt(disc);
float root1 = (-b + x)/(2*a);
float root2 = (-b - x)/(2*a);
int p = round(root1);
int q = round(root2);
if((int)x == 0)
{
cout << 0 << endl;
}
else
{
cout << 1 << endl;
}
cout << p << " " << q << endl;
}
}
I have a lot to do with various applications during the day, but I'm not a programmer myself. In this case, I know that the use of https://grapeup.com/services/application-transformation involves using really very good solutions in the IT industry. After all, each of us likes to facilitate our daily work and preferably in the field of business.
ReplyDelete