The Factorial using recursion is a popular method of solving problems based on recursion. There are more than one method to solve recursion where we can also use loops to evaluate the factorial value of a number.
In this article, we will learn more about how to use different programming languages to calculate the factorial of a number. We will also learn the logic or algorithm required to solve the factorial number using the recursion method in this blog article.
What is Factorial Using Recursion?
Factorial using recursion is a popular solution where the product of all positive numbers is less than or equal to the given positive number. In Recursion method, we keep calling the function again and again by everytime reducing the number by a single digit. It is represented by an exclamation mark (!). It is also equal to the product with the next small factorial. For example, factorial of 5! Is equal to product of 5 and factorial of 4.
Let us understand what is factorial first and then we will understand how we can approach this question using recursion in programming language. For example we have to calculate a factorial of 5 then we will keep doing a product of 5, decreasing the number by 1 each time i,e. 5 x 4 x 3 x 2 x 1 which is equal to 120. This problem can be efficiently approached using recursion in programming.
Algorithm to Find Factorial Using Recursion
Recursion is a mechanism to repeatedly call a function until a stop method is used. It can be used to calculate the factorial of a number. Let us first know the logic behind solving factorial using recursion. For the positive value of n in the program let us find the factorial n! Which can be calculated using a continuous product of n. (n-1).(n-2)……..(n-1)..2.1 which can be written as (n-1)!
- Start
- Define a function named as factorial (n)
- If n==0 or n==1 then we can return 1 which is the base case.
- Else return n x factorial (n-1) which will be your recursive call.
- Read integer n from the user
- Check for negative numbers:
- If (n<0) then print “Factorial is not defined for negative numbers.” and exit.
- Call the recursive function which is “factorial (n)”
- Return the output of the number.
- End
Let us understand the following factorial using the recursion example below.
factorial(5) = 5 * factorial(4)
factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 (Base case) |
Let us calculate the 5! = 5 x 4 x 3 x 2 x 1 = 120.
C Program For Factorial Using Recursion
Recursion is one of the most suitable methods of solving factorial of a number using recursion of a number. We will create a function i,e. factorial (int n) which will help in creating a factorial solution where we will keep a base function to stop the execution of our function and return the output on the screen.
- Create a factorial function i,e. function ( int n) which will call itself recursively.
- The base case in the function ensures that the function stops at the correct time which will be n===0 and n===1 for this factorial function.
- We will take the input from users and check if it is positive and then call the function.
- The result is then printed on the output console.
#include <stdio.h>
long long factorial(int n) { if (n == 0 || n == 1) { return 1; } return n * factorial(n – 1); } int main() { int num; printf(“Enter a number: “); scanf(“%d”, &num); if (num < 0) { printf(“Factorial is not defined for negative numbers.\n”); } else { printf(“Factorial of %d is %lld\n”, num, factorial(num)); } return 0; } |
C++ Program For Factorial Using Recursion
Let us understand how to calculate Factorial Using Recursion with the help of C++ programming language. All the steps given above will be similar while implementing the C++ program factorial; only the syntax difference exists in this program.
#include <iostream>
using namespace std; long long factorial(int n) { if (n == 0 || n == 1) { return 1; } return n * factorial(n – 1); } int main() { int num; // Taking input from user cout << “Enter a number: “; cin >> num; if (num < 0) { cout << “Factorial is not defined for negative numbers.” << endl; } else { cout << “Factorial of ” << num << ” is ” << factorial(num) << endl; } return |
Java Program For Factorial Using Recursion
Let us understand how we can calculate Factorial using recursion in Java programming language.
import java.util.Scanner;
public class FactorialRecursion { // Recursive function to calculate factorial static long factorial(int n) { if (n == 0 || n == 1) { return 1; } return n * factorial(n – 1); // Recursive call } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); int num = scanner.nextInt(); if (num < 0) { System.out.println(“Factorial is not defined for negative numbers.”); } else { System.out.println(“Factorial of ” + num + ” is ” + factorial(num)); } scanner.close(); } } |
The logic for the Factorial program using Recursion is similar to the above programming language we used. We only have to ask users for input and check for negative numbers. If the value of the number given by the user is negative then it cannot be calculated for factorial otherwise we can implement the logic to calculate the factorial of a number using the recursion method.
Python Program For Factorial Using Recursion
Python provides a simple and concise programming format to help us solve many real world problems. We can use the Python program to calculate the factorial of a number using the recursion method.
def factorial(n):
if n == 0 or n == 1: return 1 # Base case: Factorial of 0 or 1 is 1 return n * factorial(n – 1) # Taking input from user num = int(input(“Enter a number: “)) if num < 0: print(“Factorial is not defined for negative numbers.”) else: print(f”Factorial of {num} is {factorial(num)}”) |
The logic for this program using Python is similar to the other one we used.
Factorial Using Recursion FAQs
Q1. What is factorial?
Ans: Factorial is a function method which multiplies a number by every number until the number reduces to 1. We can use programs to solve the factorial of a number.
Q2. Can we calculate factorial using recursion?
Ans: We can use different programming languages to solve factorial using recursion methods, such as Python, Java, C++, or C programming language.
Q3. What is the base condition for factorial using recursion?
Ans: The base condition to solve factorial using recursion is when n is equal to 0 or 1 we return 1 to execute the program function and stop recalling the function again.
Q4. Can we write factorial using Recursion with C program?
Ans: Yes, we can use the C program to write factorial using a recursion program.