Skip to content

Instantly share code, notes, and snippets.

@hckim16
Last active February 4, 2018 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hckim16/f5d33e442783d61db6ef1943d4783073 to your computer and use it in GitHub Desktop.
Save hckim16/f5d33e442783d61db6ef1943d4783073 to your computer and use it in GitHub Desktop.
Simple recursive code for factorial solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int factorial(int n){
if(n == 0){
return 1;
}
else{
return n * factorial(n-1);
}
}
int main(){
int n;
cout << "Enter number: ";
cin >> n;
cout << "The answer is: " << factorial(n) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment