Thursday, 7 July 2016

Factorial program in Java

If we go by the definition factorial of a non-negative integer n is product of all positive integers less than or equal to n.

As example

4! = 4 X 3 X 2 X 1 = 24

If you see here you need to multiply every time by the number which is one less than the previous one until you reach 1, which can be considered base case. That makes it a very good example for learning recursive programming. In fact calculating factorial using recursion is the first program many people will do while learning recursion apart from another program How to reverse a string in Java using recursion.

If there is a recursive solution for any problem then there is always a iterative solution too so there is a iterative solution too for calculating factorial. In this post we'll see both ways to calculate factorial.

Factorial of a number in Java using Iteration

import java.util.Scanner;

public class FactorialItr {

 public static void main(String[] args) {
     // get input from user
     Scanner input = new Scanner(System.in);
     System.out.println("Enter a number: ");
     int num = input.nextInt();
     int fact = calculateFactorial(num);
     System.out.println("Factorial of " + num + " is " + fact); 

 }
 
 private static int calculateFactorial(int num){
  int fact = 1;
  for(int i = num; i >= 1; i--){
   fact = fact * i; 
  }
  return fact;
  
 }
}

Output

Enter a number: 
5
Factorial of 5 is 120

Logic here is to take input from console and calculate factorial of that number using for loop that ranges from number till 1 and within the loop keep multiplying the result with the loop counter.

Factorial of a number in Java using Recursion

import java.util.Scanner;

public class Factorial {

 public static void main(String[] args) {
     // get input from user
     Scanner input = new Scanner(System.in);
     System.out.println("Enter a number: ");
     int num = input.nextInt();
     int fact = calculateFactorial(num);
     System.out.println("Factorial of " + num + " is " + fact); 

 }
 
 private static int calculateFactorial(int num){
  // base case (exit recursion)
  if(num == 1){
   return 1;
  }else{
   return num * calculateFactorial(num - 1);
  }
  
 }

}

Output

Enter a number: 
4
Factorial of 4 is 24

Here again the user is asked to enter a number then the same method is called recursively using passed number minus one every time. Base case here is when number becomes 1. Since factorial of 0 and 1 both is 1 so both can be used as base cases.

A word of caution here though, if you are trying to get a factorial of a number more than 16 or 17 then take the type as double or BigInteger otherwise you will get an overflow error.

That's all for this topic Factorial program in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Displaying prime numbers - Java program
  2. How to display pyramid patterns in Java
  3. Print odd-even numbers using threads and wait-notify
  4. Armstrong number
  5. Fibonacci series program in Java

You may also like -

>>>Go to Java Programs page

No comments:

Post a Comment