forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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. | ||
If you have questions, try to connect with me: pezy<urbancpz@gmail.com> | ||
================================================================================= | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
void factorial_with_interacts() { | ||
int num; | ||
std::cout << "Please input a positive number: "; | ||
while ( std::cin >> num && num < 0 ) | ||
std::cout << "Please input a positive number again: "; | ||
std::cout << num; | ||
|
||
unsigned long long result = 1; | ||
while (num > 1) result *= num--; | ||
|
||
std::cout << "! is "; | ||
if ( result ) std::cout << result << std::endl; | ||
else std::cout << "too big" << std::endl; | ||
} | ||
|
||
int main() | ||
{ | ||
factorial_with_interacts(); | ||
} |