Algorithm
Bhaskara's Formula
Adapted by Neilor Tonin, URI Brazil
Read 3 floating-point numbers. After, print the roots of bhaskara’s formula. If it's 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 (double) A, B and C.
Output
Print the result with 5 digits after the decimal point or the message if it is impossible to calculate.
Input Samples | Output Samples |
10.0 20.1 5.1 |
R1 = -0.29788 |
0.0 20.0 5.0 |
Impossivel calcular |
10.3 203.0 5.0 |
R1 = -0.02466 |
10.0 3.0 5.0 |
Impossivel calcular |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main(){
double a,b,c,t,R1,R2;
cin>>a>>b>>c;
if(((b*b)-4*a*c)<0 ||a==0){
cout<<"Impossivel calcular"<<endl;
}else{
t=sqrt((b*b)-4*a*c);
R1=((-b + t) / (2 * a));
R2=((-b - t) / (2 * a));
cout<<fixed;
cout<<setprecision(5)<<"R1 = "<<R1<<endl;
cout<<setprecision(5><<"R2 = "<<R2<<endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, t;
scanf("%lf %lf %lf", &a, &b, &c);
if(((b * b) - 4 * a * c) < 0 || a == 0) {
printf("Impossivel calcular\n");
} else {
t = sqrt((b * b) - 4 * a * C);
printf("R1 = %.5lf\nR2 = %.5lf\n", ((-b + t) / (2 * a)), ((-b - t) / (2 * a))>;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double A, B, C, R1, R2;
Scanner input = new Scanner(System.in);
A = input.nextDouble();
B = input.nextDouble();
C = input.nextDouble();
if ((A == 0) || (((B * B) - (4 * A * C)) < 0)) {
System.out.print("Impossivel calcular\n");
} else {
R1 = ((-B + Math.sqrt(((B*B) -(4*A*C)))) / (2*A));
R2 = ((-B - Math.sqrt(((B*B) -(4*A*C)))) / (2*A));
System.out.printf("R1 = %.5f\n", R1);
System.out.printf("R2 = %.5f\n", R2);
}
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
import math
A,B,C = map(float,input().split())
D = (B**2)-(4*A*C)
if(D <0 or A==0):
print("Impossivel calcular")
else:
D=math.sqrt(D)
R1 = (-B+D)/(2*A)
R2 = (-B-D)/(2*A)
print(f'R1 = {R1:0.5f}\nR2 = {R2:0.5f}'>
Copy The Code &
Try With Live Editor
Input
Output