Sunday 19 May 2013

Java Program to find factorial of number in Java - Example Tutorial

How to find factorial of a number in Java on both recursive and iterative way is a common Java interview question mostly asked at fresher level. It’s not just popular on Java interview but also on other programming language like C or C++. Its also famous  In our last article we have seen how to check if a number is prime or not and in this Java programming tutorial we will see a simple Java program to find factorial of a number in Java by using recursion and iteration. Same program can also be used to print factorial of any number or printing a range of factorial as well. Let’s talk about logic, factorial of a number is calculated by formula number*(number -1) till zero and since value of factorial zero is 1, it acts as a base case in recursive version of factorial method. logic to find factorial of number is encapsulated inside factorial(int number) and fact(int number) method.

This Java programming tutorial is in next with my earlier tutorial for beginners like Java Program to reverse String in Java with recursion , Java program to free memory in java and recently  how to read and write on text file in Java etc, if you haven’t read them you may find them interesting and useful.


How to find factorial of number in Java using recursion and iteration :

How to print factorial of number in Java with ExampleIn this section we will see complete code example of Java program to calculate factorial of a number in both recursive and iterative way. If you look at closely you will find that recursive solution of calculating factorial is much easier to write and pretty intuitive to read as it represent formula number*(number -1). Any way recursion should only be limited for educational purpose as it is always subject to stack overflow error if recursion is too deep. Calculating factorial of a number is also a good programming question to exercise for anyone who is just started learning programming. Anyway here is example of Java program to find factorial of a number :

/**
 * Simple Java program to find factorial of a number using recursion and iteration.
 * Iteration will use for loop while recursion will call method itself
 */

public class FactorialInJava{

    public static void main(String args[]) {
     
      //finding factorial of a number in Java using recursion - Example
      System.out.println("factorial of 5 using recursion in Java is: " + factorial(5));
     
      //finding factorial of a number in Java using Iteration - Example
       System.out.println("factorial of 6 using iteration in Java is: " + fact(6));
    }
 
 
    /*
     * Java program example to find factorial of a number using recursion
     * @return factorial of number
     */

    public static int factorial(int number){      
        //base case
        if(number == 0){
            return 1;
        }
        return number*factorial(number -1); //is this tail-recursion?
    }
 
    /*
     * Java program example to calculate factorial using while loop or iteration
     * @return factorial of number
     */

 
    public static int fact(int number){
        int result = 1;
        while(number != 0){
            result = result*number;
            number--;
        }
     
        return result;
    }
}

Output:
 factorial of 5 using recursion in Java is: 120
 factorial of 6 using iteration in Java is: 720

That's all on how to calculate or find factorial of number in Java. Both iterative and recursive version of factorial can be used to calculate factorials but beware of StackOverFlowError if a larger number is passed to recursive version of factorial method in Java.
Other Java Programming tutorial you may like

Java Program to sort ArrayList in descending order

Java Program to loop Map in Java

Java Program to create Thread in Java

Java Program to stop Thread in Java

Java Program to parse XML file in Java using DOM

Java Program to find if array contains duplicates

Difference between throw and throws in Java

Please share with your friends if like this article

No comments:

Post a Comment