In this HackerRank Day 9: Recursion 3 problem solution we have given a function with an integer parameter. we need to calculate the factorial of it.

HackerRank Day 9: Recursion 3 problem solution

Problem solution in Python programming.


def factorial(N):
    if N < 2:
        return 1
    else:
        return N*factorial(N-1)
    
print(factorial(int(input().strip())))



Problem solution in Java programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static int factorial(int n){
        if(n==1){
            return n;
        }else{
            return n*factorial(n-1);
        }
    }
    
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        s.close();
        int result = factorial(n);
        System.out.println(result);
    }
}




Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int factorial(int number)
 {
    if (number !=0)
        {
        
        return number*factorial(number-1);
        }
    else
        {
            return 1;       
         }
    
}



int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int number;
    cin >> number;
    int y = factorial(number);
    cout << y << endl;
    
    return 0;
}



Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int i;
    scanf("%d", &i);
    printf("%d\n", factorial(i));
    return 0;
};
int factorial(int n){
        if (n <=1) return 1;
        return n * factorial(n-1);
    }



Problem solution in Javascript programming.

function processData(input) {
    var factorialResult = factorial(input);
    console.log(factorialResult);
} 

function factorial(n) {
    n = parseInt(n);
    
    if (n === 1) {
        return 1;
    } else  {
        return n * factorial(n-1)
    }

}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});