Java Factorial

  1. Factorial using Recursion in Java
  2. Factorial without using Recursion in Java
  3. Find Factorised number in Java
  4. Difference between two Factorials in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Factorial using Recursion in Java


Factorial_Ex1.java

import java.util.Scanner;
 
public class Factorial_Ex1 {
	
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter the number : ");
		
		int n = Integer.parseInt(scan.nextLine());
		
		int factorial = fact(n);
		
		System.out.println("The Factorial is : " + factorial);
	}
	
	private static int fact(int x) {
		
		if (x > 1)
		    x = x * fact(x-1);
		
		return x;
	}
}

Sample Output

Enter the number : 
8
The Factorial is : 40320




B. Factorial without using Recursion in Java


Factorial_Ex2.java

import java.util.Scanner;
 
public class Factorial_Ex2 {
	
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter the number : ");
		
		int n = Integer.parseInt(scan.nextLine());
		int fact = 1;
		
		while(n > 1) {
			
			fact = fact * n;
			n--;
		}
		
		System.out.println("The Factorial is : " + fact);
	}
}

Sample Output

Enter the number : 
9
The Factorial is : 362880




C. Find Factorised number in Java


Factorial_Ex3.java

import java.util.Scanner;
 
public class Factorial_Ex3 {
 
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter the number : ");
		
		int fact = Integer.parseInt(scan.nextLine());
		int fact_temp = fact;
		int temp = 1;
		
		for (int i=2; i>1; i++) {
						
			if((fact/i) != 0 ) {
				
				fact = fact/i;
				temp = temp * i;
			}
		        else {
				
			    if(temp == fact_temp) {
				
				System.out.println("This is factorial for : " + (i-1));
				break;
			    }
			    else {
				System.out.println("It is not a factorised number..");
				break;
			    }
			}
		}
	}
}

Sample Output

Enter the number : 
3628800
This is factorial for : 10
Enter the number : 
3628801
It is not a factorised number..




D. Difference between two Factorials in Java


Factorial_Ex4.java

import java.util.Scanner;
 
public class Factorial_Ex4 {
	
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter First Number : ");
		int first = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter Second Number : ");
		int second = Integer.parseInt(scan.nextLine());
		
		int factorial1 = fact(first);
		int factorial2 = fact(second);
		
		int diff = factorial1 - factorial2;
		
		System.out.println("The Difference is : " + diff);
	}
	
	private static int fact(int x) {
		
		if (x > 1)
		    x = x * fact(x-1);
		
		return x;
	}
}

Sample Output

Enter First Number : 
5
Enter Second Number : 
4
The Difference is : 96






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com