There are multiple solutions on how to find the factorial of a number in java. However we would be tackling the most simplified approach.

Brief Background on Factorial

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5! = 5 * 4 * 3 * 2 * 1 = 120

The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra, and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects).

Find the factorial using java loop

This solution would be leveraging the most basic and fundamental functionality of java which is the for loop.

package com.javatutorialhq.java.examples;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * This example source code demonstrates how
 * to get the factorial of a number
 */

public class NumberFactorialExample {

	public static void main(String[] args) {

		System.out.print("Enter a number:");
		BufferedReader br = new BufferedReader(new 
				InputStreamReader(System.in));
		try {
			Integer value = Integer.parseInt(br.readLine());
			Integer factorial = 1;
			for(int i=value;i!=0;i--){
				factorial = factorial * i;
			}
			System.out.println(value+"! = "+factorial);
			
		} catch (NumberFormatException | IOException e) {
			
			e.printStackTrace();
		}
		
	}

}

Sample Output Number Factorial:

Enter a number:6
6! = 720