Advertisement
advictoriam

Untitled

Jan 11th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.    Computes n! = 1 x 2 x ... (n - 1) x n, given n.
  5.    We assume n is greater than zero and less than 12.
  6.  
  7.    Input: n, the integer value for which n! is to be computed.
  8.    Output: n! -- the factorial of n
  9. */
  10. public class Factorial
  11. {
  12.    public static void main(String[] args)
  13.    {
  14.       // Read value for n
  15.       Scanner in = new Scanner(System.in);
  16.       int n = in.nextInt();
  17.       if(n <= 0){System.out.println("1"); return;}
  18.       System.out.println(factorial(n));
  19.    }
  20.    public static int factorial(int n)
  21.    {
  22.       if(n == 0){return 1;}
  23.       return n * factorial(--n);
  24.    }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement