Recursion 3 Problem solution | 30 days of code| Hacker Rank


Task:-    Write a factorial function that takes positive integer , N , as paramater and prints
                the result of N! (N factorial).

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

Input Format:-

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

Constraints:-

 >    2 <= N <= 12

 >    Your submission must contain recursive function having named factorial.

Output Format:-

 Print a single integer denoting N!.

Sample Input:-    

    3

Sample Output:-   

    6
     
Solution Code:-
  
              Note:--   Code is in Java.

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

public class Solution {
    public static void main (String[]args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); 
         int res  = factorial(n);
          System.out.println(res);
        
    }

     public static int factorial(int n)
    {
        if(n==1)
        return 1;
        else{
            return (n*factorial(n-1)); 
        }
    }
}
 
Note: - Code is in Python 3

factorial = lambda x : 1 if x<=1 else x*factorial(x-1)
print(factorial(int(input())))
 Note:- Code is in Python 2

factorial = lambda x : 1 if x<=1 else x*factorial(x-1)
print(factorial(int(raw_input())))

ABOUT

Contact By:-- ---   charlievishwakarma.2503@gmail.com
                                      
Created By:------ shubham vishwakarma(Naime.com) 

Comments

Popular posts from this blog

List problem solution |PYTHON| HackerRank

Nested lists problem solution | Python | HackerRank

String Validator problem solution |PYTHON| HackerRank