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
Complete the factorial function in the editor below. Be sure to use recursion.
a factorial has the following parameter:
Returns
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
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
Tip: Always try to implement your own logic this will help you to solve and build a logic. Before copying the program I recommended please read this full article, this will help you to build your own logic.
Submit Your Solution Here: Click Here
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
Tip: Always try to implement your own logic this will help you to solve and build a logic. Before copying the program I recommended please read this full article, this will help you to build your own logic.
Submit Your Solution Here: Click Here
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:
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.
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.
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.
0 Comments: