Factorial Program in Java using Recursion
--
Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to be calculated is represented by p!.It is pronounced as p factorial. In this section, we will create a Java program to calculate the factorial of a number using iterative and recursive approach.
Factorial of any number is the multiplication of numbers from that particular number to 1 either in increasing or in decreasing order. In general terms, we can represent the factorial of a number as follows:
p! = p × (p-1) × (p-2) × … × 1 <em>or </em>1 × … × (p-2) × (p-1) × p
For example:
3! = 3 × 2 × 1 or 1 × 2 × 3 = 6
4! = 4 × 3 × 2 × 1 or 1 × 2 × 3 × 4 = 24
Note: As per standard mathematical definition, the factorial of any negative number cannot be found. Also, 0! is always 1.
Let’s write a Java program that calculates the factorial of a number using Java for loop.