Step Five  Loops: factorial

Loops are a common thing to do in programming. If there is something you need to do over and over again, loops are the ticket. There are generally four types of loops. "for" loops, "while" loops, "do-while" loops, and finally recursion. "for" loops work a fixed number of times, "while" loops execute until some condition is no longer met, "do-while" loops execute at least once, and each recursive function has a repeating and a terminating condition.

For example, this "for" loop will print the first 10 counting numbers:

for (int ii = 1 ; ii < 11 ; ++ii) {
  cout << ii << endl;
}
The loop variable is called ii (so it stands out from all other variable names). It starts at 1, keeps going while it is less than 11, and each time through the loop, ii is increased by one. The "count progression" of ii is 1,2,3,4,5,6,7,8,9,10.

This "while" loop will do the same thing:

int ii = 1;
while (ii < 11) {
  cout << ii << endl;
  ++ii;
}
This "do-while" loop will do the same thing:
int ii = 1;
do {
  cout << ii << endl;
  ++ii;
while (ii < 11);

What if you want to print out the first 10 odd numbers? The following loop would do the job:

for (int ii = 1 ; ii < 20 ; ii+=2) :
  cout << ii << endl;
}
The count progression of ii is 1,3,5,7,9,11,13,15,17,19. Of course, this one would do the same thing:
for (int ii = 1 ; ii <= 10 ; ++ii) {
  cout << (2*ii - 1) << endl;
}
Here the count progression is 1,2,3,4,5,6,7,8,9,10. If you wanted to count down from 10 to 1 you could do that also:
for (int ii = 10 ; ii > 0 ; --ii) {
  cout << ii << endl;
}
The count progression of ii is 10,9,8,7,6,5,4,3,2,1. This loop would accomplish the same thing:
for (int ii = 1 ; ii <= 10 ; ++ii) {
  cout << 11 - ii << endl;
}

factorial.cpp

Design a program called factorial.cpp to accept the input of one positive integer value and output the factorial of that number.


1! = 1
2! = (2)(1) = 2
3! = (3)(2)(1) = 6
4! = (4)(3)(2)(1) = 24
5! = (5)(4)!
... and so on, or:
n! = (n)(n-1)! in general

Part 1:
Use a "while" loop to calculate the factorial

Part 2:
Use a "for" loop to calculate the factorial

Part 3:
Create a separate recursive function to calculate the factorial

int factorial(int number) {
  if (number == 1) return 1; //terminating condition
  else return number * factorial(number - 1);  //continuing condition
}

Example: factorial(5) is evaluated this way:

factorial(5) returns 5*factorial(4)
                       factorial(4) returns 4*factorial(3)
                                              factorial(3) returns 3*factorial(2)
                                                                     factorial(2) returns 2*factorial(1)
                                                                                            factorial(1) returns 1
So the result is 5*4*3*2*1 or 120


The "factorial" gets really big really fast. Try your program with large numbers (large means about three dozen) and see what happens.

Questions:

  1. What is the largest factorial you can evaluate using an int without a negative result?
  2. Why is the answer sometimes negative?
  3. What is the largest factorial you can evaluate using an int before the result remains 0?
  4. What is the largest factorial you can evaluate using a double?
  5. What happens with doubles bigger than in the previous question?
  6. What is the largest factorial you can evaluate using an "unsigned long long"?

Advance to Step Six

Return to the Table of Contents