Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static int factorial (int n){
  5. int result = 1;
  6. if (n==0)
  7. return 1;
  8. else
  9. for (int i=1; i<=n; i++) {
  10. result*=i;
  11. }
  12. return result;
  13. }
  14. public static void main(String[] args){
  15.  
  16. System.out.println("Podaj liczbę naturalną z przedziału [0;16]: ");
  17. Scanner input = new Scanner(System.in);
  18.  
  19. try {
  20.  
  21. int number = input.nextInt();
  22. if (number > 0 && number <= 16)
  23. System.out.println(factorial(number));
  24. else
  25. System.out.println("Liczba poza przedziałem");
  26. }
  27. catch (Exception e) {
  28. System.out.println("Error");
  29. }
  30.  
  31. }
  32. }
  33.  
  34. ///w try piszmy co ma robić jak nie ma błędu
  35. ///catch zajmuje się wyjątkami
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement