Home30 Days Of CodeHackerRank Day 9 : Recursion 3 30 days of code solution

HackerRank Day 9 : Recursion 3 30 days of code solution

Today we are going to solve  Hackerrank Day 9 : Recursion 3 30 days of code solution  in C, C++ , Java , Python & Javascript.

Objective

Today, we are learning about an algorithmic concept called recursion.

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

factorial has the following paramter:

  • int n: an integer

Returns

  • int: the factorial of n

Note: If you fail 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).

Constraints

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

Sample Input

3

Sample Output

6

Explanation

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

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

HackerRank Day 9 : Recursion 3 30 days of code solution

Recursion 3 HackerRank Solution in C

import java.io.*;
import java.util.*;

public class Solution {

    public static int factorial(int n){
        return (n > 1) ? n * factorial(n-1) : 1;
    }
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.close();
        System.out.println(factorial(n));
    }
}

Recursion 3 HackerRank Solution in C ++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int factorial(int n){
    int r;
    if(n != 1){
        r = n * factorial(n-1);
    }
    else
        r = 1;
    return r;
}

int main() {
    int n;
    cin >> n;
    int r = factorial(n);
    cout << r << endl;
    return 0;
}

Recursion 3 HackerRank Solution in Java

import java.io.*;
import java.util.*;

public class Solution {

    public static int factorial(int n){
        return (n > 1) ? n * factorial(n-1) : 1;
    }
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.close();
        System.out.println(factorial(n));
    }
}

Recursion 3 HackerRank Solution in Python 3

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the factorial function below.
def factorial(n):

    if n == 1:
        return 1
    else:
        n = n * factorial(n-1)
    return n


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

    n = int(input())

    result = factorial(n)

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

    fptr.close()

Recursion 3 HackerRank Solution in JavaScript

function processData(input) {
    //Enter your code here
    var N = parseInt(input),
        num = 1;
    
    function factorial(N){
        if (N > 1){
            num = num * N;
            factorial(N-1);
        }
    }
    factorial(N);
    console.log(num);
} 

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

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

NEXT : Hackerrank Day 10 : Binary Numbers 30 days of code solution

30 Days of Code HackerRank Solutions List – Day 0 to Day 29


Read More –

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

- Advertisment -

Categories