Practice with Loops

 

Several people have asked for some practice problems to help get ready for the first quiz on loops. Here are a few such problems: one that uses WHILE loop and one that uses FOR loops. If you can do these you should know what you need to know for the quiz:

A. Write a program that lets the user enter numbers, one number at a time, until the user finally enters 0. The program should then print out the average of these numbers. For example:
         Enter a number: 3
         Enter a number: 4
         Enter a number: 8
         Enter a number: 0
         The average of those 3 numbers is 5.0

Of course, to find the average you need to keep a running total of all of the numbers that have been entered, and also a count of how many non-zero numbers you have seen.

B. Write a program that reads a single number and then prints a line with this many * characters. For example:

        How many *'s would you like? 10
        **********