Misc-Java-Examples

How to find the factorial of a number

Posted On
Posted By admin

In this post, I will be showing you how to find the factorial of a number in Java. The method used in this example does not use recursion.

The following code snippet demonstrates how to find the factorial of the given number:.


private static int findFactorial(int num){
int result = 1;
for(int i =1; i <= num;i++){
result = result * i;
}

return result;

}

So this method iterates from 1 till the input number. It multiplies the current value of i to the result. It continues this till the input number is reached. So if you run this code for the number 5, you will get the result as 120.

 

 

 

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my Java course here

Also, if you'd like to test your Java knowledge, do check out my practice tests course here


If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment