Advertisement
illpastethat

Factorial

Mar 8th, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class Factorial {
  5.     public static int facIter(int n) {
  6.         int res = 1;
  7.         while (n > 1) {
  8.             res = res * n;
  9.             n--;
  10.         }
  11.         return res;
  12.     }
  13.     public static int facRecur(int n) {
  14.         if (n <= 1) {
  15.             return 1;
  16.         }
  17.         else {
  18.             return n * facRecur(n-1);
  19.         }
  20.     }
  21.     public static void main(String[] args) {
  22.         Scanner kb = new Scanner(System.in);
  23.         System.out.print("Enter a line of pascals triangle: ");
  24.         int input = kb.nextInt();
  25.         System.out.println("Iterative:\t" + facIter(input));
  26.         System.out.println("Recursive:\t" + facRecur(input));
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement