Advertisement
brilliant_moves

Factorial.java

Sep 7th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.75 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Factorial {
  4.  
  5.     /**
  6.     *   Program:    Factorial.java
  7.     *   Purpose:    Yahoo! Answers. Find factorial of numbers between 1 and 12.
  8.     *   Notes:      For bigger numbers, see my BigFactorial program
  9.     *   Creator:    Chris Clarke
  10.     *   Created:    22.05.2012
  11.     */
  12.  
  13.     public static void main(String[] args) {
  14.         Scanner keyboard = new Scanner(System.in);
  15.         System.out.print( "Enter a number (between 1 and 12) for factorial: ");
  16.         int n = keyboard.nextInt();
  17.         if (n>12) {
  18.             System.out.println ("Number too big!");
  19.         } else if (n<1) {
  20.             System.out.println ("Number too small!");
  21.         } else {
  22.             System.out.println( n + "! = " + factorial(n));
  23.         }
  24.     }
  25.  
  26.     public static int factorial(int n) {
  27.         return (n == 1) ? 1 : n * factorial(n - 1);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement