# Day 9 Recursion Hackerrank Solution in C | 30 Days of Code

For the day 9 recursion solution first you have to know What is Recursion?. The syntax of Recursion etc. So first we will define a definition of Recursion and then the syntax of Recursion after that with the help of an example we will solve a problem. For Recursion first, we need to know the function and declare the function above the main() function and after the declaration, we have to define the function and most important call the function by call by value or call by reference.

Objective

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

Recursive Method for Calculating Factorial

Function Description

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

a factorial has the following parameter:

• int n: an integer

Returns

• int: the factorial of

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 steps 1 to 3, results are accumulated from steps 3 to 1.

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

## Day 9 Recursion Hackerrank Solution in C

``````#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;
int factorial(int num);

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

int factorial(int num) {
if (num == 1) {
return 1;
} else {
return (num * (factorial(num - 1)));
}
}``````

### What is Recursion

According to Wikipedia Recursion occurs when a thing is defined in terms of itself or of its type. or in simple words in terms of Function, when the Function Call itself is Called Recursion

The syntax of Recursion:

So as we already discuss when a function calls it self-called Recursion.

``````void Recursion();
int main() {
Recursion()
}

void Recursion() {
Recursion();
}``````

### Explanation of Recursion Hackerrank Solution

So we are taking an example of the factorial program with Recursion. Below is the recursion program of factorial. Let's take a value of num as 5, as we know that if we want to the factorial of any number we need to multiply all numbers from 1 to number like 5 * 4 * 3 * 2 * 1 = 120.

``````int factorial(int num) // fuction num = 5
{
if (num == 1) {
return 1;
} else {
return (num * (factorial(num - 1))); //Recursion
}
}``````

In the return statement Here 5 * (factorial(num-1)) means 5 * 4 and again number reduces by 1 and 5 * 4 * 3 and repeats the process again and again. So this way we can find our solution. I hope you got the problem statement and logic of the problem, Now we can solve the problem. try to solve the Fibonacci problem with the help of recursion.

### Similar to Recursion 30 Days of Code

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.