URI Online Judge | 1036Timelimit: 1
Fórmula de Bhaskara
Adaptado por Neilor Tonin, URI
Brasil
BrasilLeia 3 valores de ponto flutuante e efetue o cálculo das raízes da equação de Bhaskara. Se não for possível calcular as raízes, mostre a mensagem correspondente “Impossivel calcular”, caso haja uma divisão por 0 ou raiz de numero negativo.
Entrada
Leia três valores de ponto flutuante (double) A, B e C.
Saída
Se não houver possibilidade de calcular as raízes, apresente a mensagem "Impossivel calcular". Caso contrário, imprima o resultado das raízes com 5 dígitos após o ponto, com uma mensagem correspondente conforme exemplo abaixo. Imprima sempre o final de linha após cada mensagem.
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner entrada = new Scanner(System.in);
double A,B,C,DELTA;
A = entrada.nextDouble();
B = entrada.nextDouble();
C = entrada.nextDouble();
DELTA = Math.pow(B,2) - (4 * A * C);
if ((A==0) || (DELTA < 0)) {
System.out.println("Impossivel calcular");
}
else {
double R1 = (((-1 * B) + Math.pow(DELTA, 0.5)) / (2*A));
double R2 = (((-1 * B) - Math.pow(DELTA, 0.5)) / (2*A));
System.out.printf("R1 = %.5f\n",R1);
System.out.printf("R2 = %.5f\n",R2);
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner entrada = new Scanner(System.in);
double A,B,C,DELTA;
A = entrada.nextDouble();
B = entrada.nextDouble();
C = entrada.nextDouble();
DELTA = Math.pow(B,2) - (4 * A * C);
if ((A==0) || (DELTA < 0)) {
System.out.println("Impossivel calcular");
}
else {
double R1 = (((-1 * B) + Math.pow(DELTA, 0.5)) / (2*A));
double R2 = (((-1 * B) - Math.pow(DELTA, 0.5)) / (2*A));
System.out.printf("R1 = %.5f\n",R1);
System.out.printf("R2 = %.5f\n",R2);
}
}
}
Nenhum comentário:
Postar um comentário