[URI Online Judge] – 1036 – Bhaskara’s Formula

Read 3 floating-point numbers. After, print the roots of bhaskara’s formula. If was impossible to calculate the roots because a division by zero or a square root of a negative number, presents the message“Impossivel calcular”.

Input

Read 3 floating-point numbers A, B and C.

Output

Print the result with 5 digits after the decimal point or the message if it is impossible to calculate.

Sample Inputs Sample Outputs
10.0 20.1 5.1 R1 = -0.29788
R2 = -1.71212
0.0 20.0 5.0 Impossivel calcular
10.3 203.0 5.0 R1 = -0.02466
R2 = -19.68408
10.0 3.0 5.0 Impossivel calcular

Solution:

#include <cstdio>
#include <cmath>
int main() {
 double a, b, c, delta;
scanf("%lf %lf %lf", &a, &b, &c);
delta = (b * b) - 4 * a * c;
 if (delta >= 0 && a != 0) {
 printf("R1 = %.5lf\n", ((b * -1) + sqrt(delta)) / (2 * a));
 printf("R2 = %.5lf\n", ((b * -1) - sqrt(delta)) / (2 * a));
 } else {
 printf("Impossivel calcular\n");
 }
return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s