# Factorial Program In Java Using For Loop

Last Updated : Mar 11, 2024

In this article we will show you the solution of factorial program in java using for loop, a number's factor is represented by the symbol n! and is defined as the sum of all positive descending integers.

Just follow the step by step guide below.

## Step By Step Guide On Factorial Program In Java Using For Loop :-

The following recursive formula can be used to calculate factorial, where the recursive call is made to a multiplicity of all the numbers smaller than the number for which the factorial is computed:

The sum of all positive descending integers is the factorial of n. The symbol for the n! factorial is n. For instance:

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

4! is pronounced as "4 factorial" in this context. It is also called as "4 bang" or "4 shriek."

Typically, combinations and permutations employ the factorial (mathematics).

The Java factorial programme can be written in a variety of ways. Let's examine the two approaches to writing a Java factorial programme.

Using a loop, a factoring programme

Recursive Factorial Programming

Before getting to the primary issue, let's quickly go through recursion. Recursion is a way for addressing problems that calls another procedure within itself.

Recursion divides a problem into more manageable subproblems and employs repeated method calls to address each one. In the end, the answer is obtained by combining the solutions to these subproblems.

The product of all the numbers from 1 to N is the factorial of the integer N. The following formula can be used to find the factorial of any number N:

Initialize the result variable to 1.

Run a for loop from 1 to N, multiplying each number with the result variable to update it.

### Algorithm :-

- Start
- Create an instance of the Scanner Class.
- Declare a variable.
- Ask the user to initialize the variable.
- Kindly Declare a variable to correctly store the factorial of the number.
- Initialize the variable to 1.
- Use a for loop to calculate the factorial.
- Update the factorial variable by multiplying it with the loop variable in each iteration.
- Print the factorial of the number.
- Stop.

This programme will calculate the factorial for a given number. FactorialNumber, a well-known function, is defined with the keyword public.

Public means that the class is available to everyone inside the programme. The main() method is used in this class. Two String class variables are present in the main() function. Which are:

int factorial = number, int number = 4,

Here, two integer-type variables with string values are stored in two variables.

The county variable I is initialised as number 1 within this loop, and the loop will continue until (i>1) is reached. Now, a loop must be built (here for loop).

class Test { static int factorial(int n) { int res = 1, i; for (i = 2; i <= n; i++) res *= i; return res; } // Driver method public static void main(String[] args) { int num = 5; System.out.println( "Factorial of " + num + " is " + factorial(5)); } }

- First step, creating a class Test
- In the next step, we will create a data variable static int factorial( int n)
- In the next step we will use for loop using int and return the result into res.
- After that we will create the main method.
- Assign a number as mentioned above int num=5.
- Lastly print the above statement.
- Run the code

## Conclusion :-

So, in this section of factorial we learned about calculating factorial of a number in Java using iteration for loop.

In the next tutorial we will learn about calculating factorial of a number in Java using do while loop.

I hope this article on factorial program in java using for loop helps you and the steps and method mentioned above are easy to follow and implement.