Java code to find Factorial of Number

Here we will see how to find factorial of a number. It’s the repeated multiplication of numbers below it.

What is Factorial of Number

Its the multiplication all natural numbers below given number. It is denoted by symbol “!”.

So if you want to find the factorial of number N, just multiply all the natural numbers below N upto 1. Note while finding factorial don’t consider ZERO as whole procduct will become ZERO itself.

Examples:

Example 1:

Find the factorial of 5

well factorial is writeen as 5!

5!= 5x4x3x2x1 = 120

So in above examle we have multiplied numbers below 5 untill 1, which gives 120 as the answer

Example 2:

Find the factorial of 8

8! = 8x7x6x5x4x3x2x1 = 40320

Here also we did the same thing that is multiplying all the numbers below 8 untill 1.

Algorithm

  1. Get the number from user for which we wish to find the factorial.
  2. Use a loop which runs from given number N to 1, in loop multiply all the below numbers of N.
  3. Once we reach lowest number 1, just print the multiplication result
  4. Congrats just now you learn’t how to find factorial, keep it up

Also Read

code for finding factors of a Number

Java Code for Fibonacci Series

Tech Mahindra off Campus Coding Question

Java Code for factorial of number

package arrayproblems;
import java.util.*;

public class factorial {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter the number to find factorial");
		int N= sc.nextInt();
		int fact=1;
		
		for(int i=1;i<=N;i++) {
			
			fact=fact*i;
			
		}
		System.out.println("Factorial of given number " + N + " is " + fact);
		

	}

}

Get step by step explanation for above code on our channel

Leave a Comment

Your email address will not be published. Required fields are marked *