Hello everyone so I'm trying to understand this code.
I'm throwing 5 into the paramter aka int x, and the function starts
5 * 4 = 20
20 * 3 = 60
60 * 2 = 120
now x is finally 1 which is == to the base case and it returns the total number we calculated after the else statement.
Am I understanding this correctly or is there a better way to understand this. Thank you in advance for your help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
using namespace std;
int factorialFinder(int x){
if(x==1){
return 1;
}else{
return x*factorialFinder(x-1);
}
}
int main()
{
cout << factorialFinder(5) << endl;
}
|
Last edited on
Sounds reasonable, and it works too! :)