forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex6_04.cpp
More file actions
31 lines (25 loc) · 671 Bytes
/
Copy pathex6_04.cpp
File metadata and controls
31 lines (25 loc) · 671 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Write a function that interacts with the user, asking for a number and
// generating the factorial of that number. Call this function from main.
// About the magic number(13): https://github.com/Mooophy/Cpp-Primer/pull/172
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
int fact(int val)
{
int ret = 1;
while (val > 1) ret *= val--;
return ret;
}
void factorial_with_interacts()
{
for (int val = 0; cout << "Enter a number within [0, 13): ", cin >> val;) {
if (val < 0 || val > 12) continue;
cout << val << "! =" << fact(val) << endl;
}
}
int main()
{
factorial_with_interacts();
}