SHARE
TWEET

Untitled

advictoriam Jan 11th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top