Home > General > Loop Exercises

Loop Exercises

For Loops

  1. Display the message “Hello World!” 25 times
  2. Change the program so that the user can decide how many times to display “Hello World” on the screen.
  3. Display the number of the repetition on the screen as well (eg. 1. Hello World!, 2. Hello World!, etc.)
  4. Ask the user which times table they wish to see, then display it in the following format:
    1 * 7 = 7
    2 * 7 = 14
    .
    .
    12 * 7 = 84
  5. Ask the user how many stars they want to see in one row. Display the row of stars
  6. Expand your solution and ask the user how many rows of stars they want to see. Display the rectangle of stars as specified by the user. Hint – you will need to nest one For loop inside another!

While Loops

  1. Ask the user to enter a whole number greater than 99. If the user enters a number less than 100, display an error message and ask them again. Repeat this process until the user enters a valid number.
  2. Expand your solution to only accept numbers between 100 and 200

Repeat Loops

  1. Add up a series of whole numbers entered by the user, until the user enters zero.
  2. Expand your solution to display the average value of the numbers entered. Hint: do not count the final zero as a value to be averaged.
Categories: General Tags:
  1. No comments yet.
  1. No trackbacks yet.