forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex6_04.cpp
36 lines (29 loc) · 941 Bytes
/
ex6_04.cpp
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
32
33
34
35
36
/*
=================================================================================
C++ Primer 5th Exercise Answer Source Code
Copyright (C) 2014-2015 github.com/pezy/CppPrimer
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
If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/
#include <iostream>
#include <string>
int fact(int val)
{
int ret = 1;
while (val > 1)
ret *= val--;
return ret;
}
void factorial_with_interacts() {
for (int val = 0; std::cout << "Enter a number within [0, 13): ", std::cin >> val; ) {
if (val < 0 || val > 12) continue;
std::cout << val << "! =" << fact(val) << std::endl;
}
}
int main()
{
factorial_with_interacts();
}