Back
Close

Computing with Data

elgeish
31.2K views

Recursion - Part III

In the example below, the recursive function contains a stopping condition (n == 1), which stops the recursion when met:

#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
cout << factorial(3) << endl; // prints 6
return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants