Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Factorial
- * Date : 25.05.19
- */
- import java.util.Scanner;
- public class Factorial {
- public static void main(String args[]) {
- Scanner input = new Scanner(System.in);
- System.out.print("Enter limit: ");
- int n = input.nextInt();
- System.out.print("Factorial numbers(Without Recursion): ");
- for(int i = 0; i <= n; i++) {
- System.out.print(fact(i) + " ");
- }
- System.out.println();
- System.out.print("Factorial numbers(With Recursion): ");
- for(int i = 0; i <= n; i++) {
- System.out.print(recFact(i) + " ");
- }
- input.close();
- }
- static long recFact(int n) {
- if(n == 0 || n == 1) return 1;
- return n*recFact(n-1);
- }
- static long fact(int n) {
- if(n == 0) return 1;
- long result = 1;
- for(int i = n; i > 0; i--) {
- result *= i;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment