fahimkamal63

Factorial numbers

May 25th, 2019
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. /*
  2.  * Factorial
  3.  * Date : 25.05.19
  4.  */
  5. import java.util.Scanner;
  6. public class Factorial {
  7.     public static void main(String args[]) {
  8.         Scanner input = new Scanner(System.in);
  9.         System.out.print("Enter limit: ");
  10.         int n = input.nextInt();
  11.         System.out.print("Factorial numbers(Without Recursion): ");
  12.         for(int i = 0; i <= n; i++) {
  13.             System.out.print(fact(i) + " ");
  14.         }
  15.        
  16.         System.out.println();
  17.         System.out.print("Factorial numbers(With Recursion): ");
  18.         for(int i = 0; i <= n; i++) {
  19.             System.out.print(recFact(i) + " ");
  20.         }
  21.         input.close();
  22.        
  23.     }
  24.    
  25.     static long recFact(int n) {
  26.         if(n == 0 || n == 1) return 1;
  27.        
  28.         return n*recFact(n-1);
  29.     }
  30.    
  31.     static long fact(int n) {
  32.         if(n == 0) return 1;
  33.         long result = 1;
  34.         for(int i = n; i > 0; i--) {
  35.             result *= i;
  36.         }
  37.         return result;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment