HackerRank Solutions – 30 Days of Code – Day 9

HackerRank Solutions – 30 Days of Code – Day 9

 

Objective
Today, we’re learning and practicing an algorithmic concept called Recursion. Check out the Tutorial tab for learning materials and an instructional video!

Recursive Method for Calculating Factorial

factorial=n*(factorial(n-1))

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

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.

Output Format

Print a single integer denoting .

Sample Input

3

Sample Output

6


PROGRAM:

#include <stdio.h>
 
int factorial(int);
 
int main()
{
    int num;
    int result;
    scanf("%d", &num);
        result = factorial(num);
        printf("%d",result);
    return 0;
}
int factorial(int num)
{
    if (num == 0 || num == 1)
    {
        return 1;
    }
    else
    {
        return(num * factorial(num - 1));
    }
}

OUTPUT:

Congratulations
You solved this challenge. Would you like to challenge your friends?

Next Challenge
Input (stdin)Download
3
Expected OutputDownload
6
Compiler Message
Success
Follow us on Social Media:

Be the first to comment

Leave a Reply

Your email address will not be published.