Bhaskara's Formula beeCrowd 1036 URI 1036 Solution C Python problem solve solution solved coding Impossivel calcular discriminant sqrt r1 r2 x1 x2 1 2
Bhaskara's Formula
Read 3 floating-point numbers (double). Print the result with 5 digits after the decimal point. if it is impossible to calculate print the message "Impossivel calcular"
The formula is:
- Take input.
- Calculate the value of discriminant(d) with the formula: b2-4ac.
- Next, if the value of discriminant(d) is less than or if the value of a is zero then print "Impossivel calcular".
- Else calculate the value of x1(R1) and x2(R2) with this formula: (-b±Ã–d)/2a. note that you need to calculate 2 values. One for plus and one for minus. And then print both values as said on that site.
The solution in C:
#include <stdio.h>
int main() {
double a,b,c,R1,R2;
scanf("%lf %lf %lf",&a,&b,&c);
double D = (b*b)-(4*a*c);
if(D<0){
printf("Impossivel calcular\n");
}
else if(a==0){
printf("Impossivel calcular\n");
}
else{
R1 = (-b+sqrt(D))/(a+a);
R2 = (-b-sqrt(D))/(a+a);
printf("R1 = %0.5f\n",R1);
printf("R2 = %0.5f\n",R2);
}
return 0;
}
The solution in Python:
import math;
a,b,c=map(float, input().split(" "))
d = ((b*b)-(4*a*c));
if d>=0:
print("Impossivel calcular")
elif a==0:
print("Impossivel calcular")
elif d>0:
x1 = ((-b+math.sqrt(d))/(2*a))
x2 = ((-b-math.sqrt(d))/(2*a))
print(f"R1 = %0.5f\nR2 = %0.5f" % (x1,x2))
COMMENTS