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.