 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Find Factorial of a Number Using Recursion
In this article, we will understand how to find the factorial of a number using recursion in Java. The factorial of a positive number is the product of the number itself and all descending positive integers up to "n".
The factorial of a negative number does not exist, and the factorial of 0 is 1. The factorial of a positive number is -
factorial of n (n!) = 1 * 2 * 3 * 4 * ... * n
Factorial Using a Recursive Function
A recursive function is a function that calls itself multiple times until a particular condition is satisfied. Recursion is the process of repeating items in a self-similar way. Given task is to find the factorial using a recursive function.
Input to the Function: 7 Resturn value: 5040
Basic Recursive Approach
The basic recursive approach involves solving a problem by dividing it into smaller subproblems of the same type. It uses a base case to terminate the recursion and avoids infinite loops.
-  Declare and initialize a variable that stores a factorial number. 
-  Define recursive function factorial(n) and Base Case: If n == 0 or n == 1, then return 1 
-  Recursive Case: Otherwise, return n * factorial(n - 1) 
-  Call the recursive function with input n and store the result. 
-  Print the factorial value. 
Example
In the given code, the factorial method calls itself with my_input - 1 to compute the factorial of smaller numbers. This recursive process continues until the base case (my_input < 1) is reached, which returns 1.
Each recursive call multiplies the current my_input by the result of the next call.
public class Factorial { public static void main(String[] args) { int my_input ; long my_result; my_input = 7; System.out.println("The number is defined as " +my_input); my_result = factorial(my_input); System.out.println("The factorial of " + my_input + " is " + my_result); } public static long factorial(int my_input) { if (my_input >= 1) return my_input * factorial(my_input - 1); else return 1; } } Following is the output of the above program -
The number is defined as 7 The factorial of 7 is 5040
