Lab 06
Version 1

 

  1. Though for loops are the preferred way to write loops when you know in advance how many times you want to loop, such loops can also be written with while loops. Write a program using a while loop which will print out the numbers from 1 to 50 and, when done, print out the sum of those numbers as well.

  2. Write a program which prompts the user for a positive number and tells them whether it is odd or even. If they do not enter a positive number, repeatedly prompt them until they do.

  3. Write a program which generates a random number between 1 and 20 and asks the user to guess it. If they guess right, they are complimented and the program ends. If they guess wrong, tell them whether they are too high or too low and go back and get another guess.

  4. Write a program based on the last one, but in which the user is limited to at most 5 guesses. (It may help you to first modify the program so that it simply keeps track of how many guesses they have made and tells them at each guess what guess number they are on.)

  5. Write a program that plays the guessing side of the guessing game. Tell the user to think of a number from one to twenty. After the program guesses a random number in that range, ask the user if it is correct. Allow for three responses: "yes","higher", or "lower". If you get one of the latter two responses, have the program guess again, but modifying the range of the guess appropriately. Repeat until the program guesses correctly, or loses for using up five guesses.

Last modified August 28 for Fall 99 cs5 by fleck@cs.hmc.edu


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Tue, Oct 6, 1998 at 11:23:24 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_06/lab.html