In this article, we will be solving HackerRank Day 9: Recursion 3 problem, part of 30 Days of Code. Majorly, we will use Python3 and Java15 to solve the problem.


Today, we’re learning about an algorithmic concept called recursion. Check out the Tutorial tab for learning materials and an instructional video.

Recursive Method for Calculating Factorial

Factorial Formula

Function Description

Complete the factorial function in the editor below. Be sure to use recursion.

Factorial has the following parameter:

  • int n: an integer


  • int: the factorial of n

Note: If you to use recursion or fail to name your recursive function factorial or Factorial, you will get a score of 0.

Input Format

A single integer, n(the argument to pass to factorial).


  • 2 <= n <= 12
  • Your submission must contain a recursive function named factorial.

Sample Input


Sample Output



Consider the following steps. After the recursive calls from steps 1 to 3, results are accumulated from steps 3 to 1.

  1. factorial(3) = 3 * factorial(2) = 3 * 2 = 6
  2. factorial(2) = 3 * factorial(1) = 2 * 1 = 2
  3. factorial(1) = 1

You can find all the source code on my GitHub profile:

Solution Day 9: Recursion 3 in Python3


import math
import os
import random
import re
import sys

# Complete the 'factorial' function below.
# The function is expected to return an INTEGER.
# The function accepts INTEGER n as parameter.

def factorial(n):
    # Write your code here
    if n == 1:
        return 1
        n = n * factorial(n-1)
    return n

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())

    result = factorial(n)

    fptr.write(str(result) + '\n')


Solution Day 9: Recursion 3 in Java15

import java.math.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import static;
import static;

class Result {

     * Complete the 'factorial' function below.
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER n as parameter.

    public static int factorial(int n) {
    // Write your code here
        return (n > 1) ? n * factorial(n-1) : 1;


public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(;
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        int result = Result.factorial(n);


HackerRank Day 9: Recursion 3 Solution | Output – 30 Days of Code

We hope that we bring some value to your life by posting our content, which might meet your expectations. You can always comment on the post to give feedback or reach out to us through email for sharing what you like to read on our blog.

Reach out to us using email: [email protected]

Find More Articles on Our Website: EGrasps

Check out our other posts on Medium.

You can reach out to us on WhatsApp.


Leave a Reply

Avatar placeholder

Your email address will not be published.