Factorial program in Java

Views: 21   

Factorial Java program which takes a number as input and prints the factorial value of it


import java.util.Scanner;

public class Factorial {

    public static void main(String[] args) {

        System.out.println("Please enter a number:");

        Scanner in = new Scanner(System.in);
        int input = in.nextInt();
        in.close();

        int result = 1;

        if (input <= 0) {
            result = 1;
        } else {
            for (int i = 1; i <= input; i++) {
                result = result * i;
            }
        }

        System.out.println("The factorial of the given number is: " + result);

    }

}

Output:

Please enter a number:
10
The factorial of the given number is: 3628800
On By