fork download
  1. import java.util.*;
  2.  
  3. class Main {
  4. public static void main(String []args) {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. try {
  8. System.out.println(primo(n) ? "Verdadeiro" : "Falso");
  9. } catch (Exception ex) {
  10. System.out.println(ex.getMessage());
  11. }
  12. }
  13. public static boolean primo(int n) throws Exception {
  14. if (n <= 0) throw new Exception("Número não pode ser menor ou igual à zero!");
  15. int contador = 0;
  16. for (int j = 1; j <= Math.sqrt(n); j++){
  17. if (n % j == 0) {
  18. contador++;
  19. if (contador == 2) return false;
  20. }
  21. }
  22. return true;
  23. }
  24. }
  25.  
  26. //https://pt.stackoverflow.com/q/148017/101
Success #stdin #stdout 0.1s 35424KB
stdin
-1

stdout
Número não pode ser menor ou igual à zero!