0

Factorial

#include <iostream> using namespace std; int factorial(int n) { if (n==1) { return 1; } else { return n * factorial(n-1); } } int main() { cout << factorial(5); } can it be done with cin so that user has to input a numbet, and could someone give an example maybe, thanks

21st Jul 2017, 6:04 PM
Nikica Ivandic
Nikica Ivandic - avatar
4 Answers
0
Sure, why not? #include <iostream> int factorial(int n) { return n <= 1 ? 1 : n * factorial(n - 1); } int main() { int n; std::cin >> n; std::cout << factorial(n) << std::endl; }
21st Jul 2017, 6:49 PM
Dennis
Dennis - avatar
21st Jul 2017, 9:37 PM
chris
chris - avatar
0
Great, thanks â˜ș
21st Jul 2017, 6:57 PM
Nikica Ivandic
Nikica Ivandic - avatar
0
I don't think you understood what I was saying
21st Jul 2017, 10:15 PM
Nikica Ivandic
Nikica Ivandic - avatar