Implementation
There are mainly two ways to write the factorial program in Java:
1.) Using Iteration
The iteration method is discussed below:
Algorithm:
The steps for calculating factorial using iteration in java are as follows 
 Initialise the result to 1
long Result = 1;
 Run a loop in the range [1, number]
for(int i = 1 ; i <= Number ; i++) {}
 In each iteration update the result.
Result = Result * i;
 Finally, print the result
System.out.printf("The Factorial of %d is: %d ",Number,Result);
Code:
public class Factorial {
public static void main(String[] args) {
int Number = 6; // Given Number
long Result = 1;
for(int i = 1 ; i <= Number ; i++) {
Result = Result * i;
}
System.out.printf("The Factorial of %d is: %d ",Number,Result);
}
}
Output:
From the above program, the factorial of 6 comes out to be 720.
Letâ€™s see the steps for number 6 in iteration.
Complexity Analysis
The time and space complexity of the above code is shown below:
Time Complexity:
The time complexity of the above code is O(n), where n is the number.
Space Complexity:
The space complexity of the above code is O(1), as we are not allocating any extra spaces.
2.) Using Recursion
The Recursion method is discussed below:
Algorithm:
The steps for calculating factorial using recursion in java are as follows:
 Create a recursive function by giving the Number as a parameter.
public static long fact(int Number) {}
 If the number is 0, return 1 else call the function for Number  1
if(Number == 0) return 1;
else return Number * fact(Number  1);
 Store the result in a variable after the end of the recursion.
long Result = fact(Number);
System.out.printf("The factorial of Number %d is: %d",Number,Result);
Code:
public class Factorial {
public static long fact(int Number) {
if (Number == 0)
return 1;
else
return Number * fact(Number  1);
}
public static void main(String[] args) {
int Number = 6;
long Result = fact(Number);
System.out.printf("The factorial of Number %d is: %d", Number, Result);
}
}
Output:
Let's see the steps for number 6 in recursion.
Try it by yourself on Java Online Compiler.
Complexity Analysis
The time and space complexity of the above code is shown below:
Time Complexity:
The time complexity of the above code is O(n), where n is the number.
Space Complexity:
The space complexity of the above code is O(n) as a stack of size n will be allocated here to store the functionsâ€™ state.
Also check out Addition of Two Numbers in Java here.
Read More  Time Complexity of Sorting Algorithms
FAQs

What is a factorial number?
The factorial of a nonnegative integer(N) is the product of integers from 1 to N.

How do you solve the factorial of 4?
4! = 4 * 3 * 2 * 1 = 24

Can we find a factorial of a negative number?
No factorial of a negative number is not possible.
letâ€™s understand why
We can write n! = (n + 1)! / (n + 1).
Like , 5! = 6! / 6 , 4! = 5! / 5.
But for negative numbers like 1.
1! = 0! / 0
It is undefined. This applies to other negative numbers as well.

What is the time complexity of the factorial program?
For both the iteration and recursion solution, the time complexity of the factorial program is O(n), where n = number.

What is the space complexity of the factorial program?
For the iteration solution, the space complexity is O(1), and for the recursion solution, the space complexity is O(n) due to the call stack.(n = Number). For factorial n, a stack of size n will be allocated to store the functions' state.
You can also read about  Strong number in c
Key Takeaways
In this article, we have extensively discussed the factorial program and its implementations in Java.
In this blog, we started with the basic definition of factorial. Then we discussed the two possible methods, i.e., iterative and recursive approaches regarding the factorial program their implementations with the proper program flow diagram.
We hope that this blog has helped you enhance your knowledge regarding the factorial program and if you would like to learn more, check out our articles on Reverse Array in Java Binary Search in Java. Do upvote our blog to help other ninjas grow.
Recommended Readings:
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!
Recommended Problems:
Happy Reading!