forked from noahc66260/C-PrimerPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpe7-5.cpp
31 lines (25 loc) · 855 Bytes
/
pe7-5.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
// pe7-5.cpp -- calculates the factorial of a number using a recursive function
// This is exercise 5 of chapter 7 in C++ Primer Plus by Stephen Prata
#include<iostream>
unsigned long int factorial(unsigned int n);
int main(void)
{
using namespace std;
cout << "Enter a number: ";
unsigned int number;
// I've chosen to make number unsigned since our data should be positive. However, I am unsure if this is best since now I cannot error check by checking to see if number is negative. On my computer, the terminal displays "Segmentation fault" and ends the program.
while (cin >> number)
{
cout << number << "! = " << factorial(number) << endl;
cout << "Another number (q to quit): ";
}
return 0;
}
// a recursive function
unsigned long int factorial(unsigned int n)
{
if (0 == n)
return 1;
else
return n * factorial(n - 1);
}