Advertisement
a1ananth

Untitled

Oct 28th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Factorial {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int[] inputArray = new int[100];
  7.         int i = 0, input, temp = 0;
  8.        
  9.         do {
  10.             System.out.print("Enter positive number (or 0) or negative number to end input : ");
  11.             input = scanner.nextInt();
  12.             inputArray[i] = input;
  13.             i++;
  14.         } while(input >= 0);
  15.        
  16.         do {
  17.             System.out.println(inputArray[temp] + " factorial is " + inputArray[temp] + "! = " + factorial(inputArray[temp]) + "\n\n");
  18.             temp++;
  19.         } while(temp <= i);
  20.     }
  21.    
  22.     public static int factorial(int n) {
  23.         if (n < 0) {
  24.             System.out.println("Negative numbers do not have factorials!");
  25.             System.exit(0);
  26.         }
  27.        
  28.         if (n == 0 || n == 1) return 1;
  29.        
  30.         return n * factorial(n-1);
  31.     }
  32. }
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement