This lab is designed to give you practice writing while loops. As usual, create a subdirectory for this lab, open up the Web version of this handout in Netscape, and open emacs.
1 I love Computer Science!! 2 I love Computer Science!! 3 I love Computer Science!! Printed this message 3 times.
Printed this message 3 times. The sum of the numbers from 1 to 3 is 6.Note that you will need to add a variable to hold the sum.
Print this program to turn in.
Here are the first 4 powers of 2: 1 2 4 8
Here are the first 4 powers of 2: 2^0 = 1 2^1 = 2 2^2 = 4 2^3 = 8
Print this program to turn in.
Write a program that asks the user for a non-negative integer and computes and prints the factorial of that integer. You'll need a while loop to do most of the work -- this is a lot like computing a sum, but it's a product instead. And you'll need to think about what should happen if the user enters 0.
Now modify your program so that it checks to see if the user entered a negative number. If so, the program should print a message saying that a nonnegative number is required and ask the user the enter another number. The program should keep doing this until the user enters a nonnegative number, after which it should compute the factorial of that number. Hint: you will need another while loop before the loop that computes the factorial. You should not need to change any of the code that computes the factorial!
Print this program to turn in.