Programming Exercise 7

  < Previous  Next >

Due Date

See the calendar for due date.

Assignment:

In mathematics, the notation n! represents the factorial of the non-negative integer n. The factorial of n is the product of all the non-negative integers from 1 to n. For example: 9! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 = 362,880.
Write a program that lets the user enter a non-negative integer and then uses a loop to calculate the factorial of that number. Print the factorial to standard output (the screen). The program should check against non-negative inputs and print an appropriate error message.

Examples

The following are examples of correct execution (with the text in bold being the input from the user):
Please enter a non-negative integer: 9
You entered 9
9! = 362,880
Please enter a non-negative integer: -7
You entered -7
That's a negative number!
Please enter a non-negative integer: 7
You entered 7
7! = 5,040
Please enter a non-negative integer: 5
You entered 5
5! = 120
Please enter a non-negative integer: 123
You entered 123
123! = 12,146,304,367,025,329,675,766,243,241,881,295,855,454,217,088,483,382,315,328,918,161,829,235,892,362,167,668,831,156,960,612,640,202,170,735,835,221,294,047,782,591,091,570,411,651,472,186,029,519,906,261,646,730,733,907,419,814,952,960,000,000,000,000,000,000,000,000,000

Submission

Submit your python script using the handin program. For handin, for this lab, type the following in a terminal window exactly as it appears:
handin  pe7  pe7.py
To verify your submission, type the following in a terminal window:
handin  pe7

Optional

Instead of just printing an error message that a non-negative number was entered, continuously ask the user for valid input until they do so. For example:
Please enter a non-negative integer: -7
You entered -7
That's a negative number!
Please enter a non-negative integer: -3
You entered -3
That's a negative number!
Please enter a non-negative integer: -1
You entered -1
That's a negative number!
Please enter a non-negative integer: 4
You entered 4
4! = 24