## AndroidBerry.com |

This topic demonstrates how to find factorial of a given number using different techniques in java.

Factorial is a multiplication from given number to 1.

Suppose given number is 5 then, we multiply 5 with all previous numbers upto 1 as.

5 * 4 * 3 * 2 * 1 = 120

Factorial is denoted by ! (Exclamation Marks)

Finding factorial of 5 using program

For iterating the multiple times we have different looping techniques. We can use any of the following loop to find the factorial

- Using for loop
- Using while loop
- Using do while loop
- Using recursion

In below example we are using a scanner class to get input from user.

import java.util.Scanner; public class FactorialEx { public static void main(String[] args) { int i,num,result=1; Scanner sc = new Scanner(System.in); System.out.print("Enter number : "); num = sc.nextInt(); for(i=num;i>=1;i--) { result = result * i; } System.out.print("Factorial of "+num+" is "+result); } }

Enter number : 5

Factorial of 5 is 120

Factorial of 5 is 120

In above example, we have started our loop from given number 5. Inside the loop we have written one statement to multiply the current interation number with previous result. The loop will continue till we get 1 in the iteration.

NOTE : The Factorial of number 0 and 1 is 1 |

import java.util.Scanner; public class FactorialEx { public static void main(String[] args) { int num,num2,result=1; Scanner sc = new Scanner(System.in); System.out.print("Enter number : "); num = sc.nextInt(); num2=num; while(num>=1) { result = result * num; num--; } System.out.print("Factorial of "+num2+" is "+result); } }

Enter number : 6

Factorial of 6 is 720

Factorial of 6 is 720

In above example, the loop checks for the condition wheather num reaches to 1 or not. if not then result is generated by multiplying the current number and then number is decremented by 1.

It is similar to the while loop, but the difference is do while loop executes at least once.

import java.util.Scanner; public class FactorialEx { public static void main(String[] args) { int num,num2,result=1; Scanner sc = new Scanner(System.in); System.out.print("Enter number : "); num = sc.nextInt(); num2=num; do { result = result * num; num --; }while(num>=1); System.out.print("Factorial of "+num2+" is "+result); } }

Enter number : 5

Factorial of 5 is 120

Factorial of 5 is 120

Above program is similar to the while loop but, Only the do keyword is used before the while loop and body is written for do keyword instead of while.

**Recursion ** is the process where the function calls itself again and again just like loops.

import java.util.Scanner; public class FactorialEx { int fact(int f) { int getnum=f; if(f==0) return 1; else { getnum = getnum * fact(f-1); return getnum; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); FactorialEx f = new FactorialEx(); int num=0; System.out.print("Enter Number to find factorial : "); num = sc.nextInt(); System.out.println("Factorial of "+num+" is "+f.fact(num)); } }

Enter Number to find factorial : 6

Factorial of 6 is 720

Factorial of 6 is 720