Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static int factorial (int n){
- int result = 1;
- if (n==0)
- return 1;
- else
- for (int i=1; i<=n; i++) {
- result*=i;
- }
- return result;
- }
- public static void main(String[] args){
- System.out.println("Podaj liczbę naturalną z przedziału [0;16]: ");
- Scanner input = new Scanner(System.in);
- try {
- int number = input.nextInt();
- if (number > 0 && number <= 16)
- System.out.println(factorial(number));
- else
- System.out.println("Liczba poza przedziałem");
- }
- catch (Exception e) {
- System.out.println("Error");
- }
- }
- }
- ///w try piszmy co ma robić jak nie ma błędu
- ///catch zajmuje się wyjątkami
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement