fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. Scanner sc = new Scanner(System.in);
  12.  
  13. int resultadoFatorial = 1;
  14. StringBuilder multiplicador = new StringBuilder();
  15.  
  16. System.out.println("Insira um número para descobrir seu respectivo Fatorial:");
  17. int fatorialDoNumero = sc.nextInt();
  18.  
  19. while (fatorialDoNumero < 0) {
  20. System.out.println("Não é possível calcular o fatorial de números negativos!");
  21. System.out.println("Insira um número maior ou igual a zero para descobrir seu respectivo Fatorial:");
  22. fatorialDoNumero = sc.nextInt();
  23. }
  24.  
  25. for (int i = fatorialDoNumero; i > 1; i--) {
  26. resultadoFatorial *= i;
  27. multiplicador.append(i).append(" x ");
  28. }
  29.  
  30. if (fatorialDoNumero == 0) {
  31. System.out.println(fatorialDoNumero + "! = 1");
  32. } else {
  33. System.out.println(fatorialDoNumero + "! = " + multiplicador + "1 = " + resultadoFatorial);
  34. }
  35.  
  36. sc.close();
  37. }
  38. }
Success #stdin #stdout 0.22s 54136KB
stdin
10
stdout
Insira um número para descobrir seu respectivo Fatorial:
10! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 3628800