Computer Science 2611
Summer 2002
Laboratory Assignment 3

Handed out: 5/23/02

Terminology

The factorial of n, written n!, is defined to be the product of all of the positive integers up to and including n. For example, 3! = 1*2*3 = 6 and 4! = 1*2*3*4 = 24. Number n! tells how many ways there are to rearrange a word that contains n different letters. For example, the word cat can be rearranged in 3! = 6 ways, as cat, cta, act, atc, tca and tac.

Part 1

Write a program that reads a number n and prints n!. It should use a loop to accumulate n!. Write your program so that it does not change the variable n that it reads. You will want to have n after computing n!. Test your program.

Part 2

Copy your program from part 1, and make a modification to the copy. The new program should read a number M and print a table of the factorials from 1 to M. For example if it reads M = 5 it should print the following table.

     n          Number of ways to rearrange n things
    ---         ------------------------------------

     1          1
     2          2
     3          6
     4          24
     5          120

Make your program print a heading as shown. Also, it should print a reasonable prompt to ask for the number M. For example, it can say "How far should the table go?" before reading M.

Write this program by building a loop around the loop that you had before. You should end up with a loop inside a loop. Just use n as a counter in the outer loop. The inner loop should compute n!. It is important that the inner loop does not change n or it will foul up the outer loop. Be sure to test your program.

What to turn in

Turn in your solution to part 2. Be sure that your name and the assignment number and version are in the program at the top, as comments.