Recursion | Sololearn: Learn to code for FREE!

0

Recursion

#include <iostream> using namespace std; int factorial(int n) { if (n==1) { return 0; } else { return n - factorial(n-1); } } int main() { cout << factorial(5); } --------------------------------------------- //output 2 ????????????????????????????

3 Answers

New Answer

+1

Use multiplication symbol instead of minus to get result.

0

5- factorial(4) 👇 (4 - factorial(3)) 👇 3 - factorial(2) 👇 2 - factorial(1) : 5 - (4 - (3 - (2 - 0))) 5 - (4 - (3 - 2)) 5 - (4 - 1) 5 - 3 2 SO it return 2. Edit : [It won't call, factorial(0)] Edit : دانیال رضایی نژاد whats your question actually? Pls give clarity..