Skip to content

Instantly share code, notes, and snippets.

@meysampg
Created November 5, 2018 06:25
Show Gist options
  • Save meysampg/9a5ccfe6f455a2ed81a23b618944ee1e to your computer and use it in GitHub Desktop.
Save meysampg/9a5ccfe6f455a2ed81a23b618944ee1e to your computer and use it in GitHub Desktop.
Recursive factorial without if :-?
#include <iostream>
bool fact(int n, int* res) {
*res *= n;
return n-1 && fact(n-1, res);
}
int main() {
int i;
for (int j = 1; i = 1, j < 6; j++) {
fact(j, &i);
std::cout << "fact(" << j << ") = " << i << std::endl;
}
return 0;
}
@meysampg
Copy link
Author

meysampg commented Nov 5, 2018

yasna@freedom:~/www/test/go/fact 
$ ./a.out     
fact(1) = 1
fact(2) = 2
fact(3) = 6
fact(4) = 24
fact(5) = 120

😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment