Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- Computes n! = 1 x 2 x ... (n - 1) x n, given n.
- We assume n is greater than zero and less than 12.
- Input: n, the integer value for which n! is to be computed.
- Output: n! -- the factorial of n
- */
- public class Factorial
- {
- public static void main(String[] args)
- {
- // Read value for n
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- if(n <= 0){System.out.println("1"); return;}
- System.out.println(factorial(n));
- }
- public static int factorial(int n)
- {
- if(n == 0){return 1;}
- return n * factorial(--n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement