Breaking News
Home / Java / Java Tutorials / Write a program to compute factorial of a number in Java

Write a program to compute factorial of a number in Java

Compute Factorial in JavaIn mathematics, the factorial of a positive integer  n  denoted by n!, is the product of all positive integers less than or equal to n. For example,

   5 ! = 1 x 2 x 3 x times 4 x 5 = 120 

0! is a special case that is explicitly defined to be 1.

The factorial operation is encountered in many different 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 a the set of objects).

This fact was known at least as early as the 12-th century, to Hindu scholars. The notation n! was introduced by Christian Kramp in 1808. – From Wikipedia.

Factorial Flowchart

The following flowchart shows how to compute the factorial of a number.

Factorial Flowchart

Factorial Program

The following Java snippet shows how to compute the factorial of any number.

 

 

Program Output

The following is the output of the above program for when the input is 5. i.e. computing the factorial of 5.

Factorial of : 5
is :120

[pb_builder]

Check Also

Have a Question

How to select a JVM Analyzing Tool?

A JVM monitoring tool also know as a Diagnostic might be needed to fine tune …

Advertisment ad adsense adlogger