Description
The purpose of this challenge is to use the WHILE loop to control program flow. This challenge will use the WHILE loop in various ways.
Requirements
Write all your code for the following steps in one file as one program. This program doesn’t really do anything except illustrates various ways to use a while loop. Each of the items below are independent of each other – they are not steps to accomplishing one program.
- Ask the user to enter a single string. Ask the user to enter an integer value for a variable called repetitions. Use a while loop to display this string (of your choice) repeated repetitions times, one string per line. Hint: declare an integer called counter, initialized to zero.
- Use a while loop to count and display all the integers from 1 to 20. Separate numbers with a space
- Ask the user to enter a number larger than 100. Then, use a while loop to display all numbers divisible by 10 starting from the number entered by the user all the way down to 0. Only show numbers divisible by 10. Separate numbers with a tab. TAB character is displayed using the \t escape sequence. (Remember that \n is a newline; use similarly).
- Use a while loop to keep asking the user for an integer as long as positive numbers are being entered. For every positive number entered, show its value multiplied by 2. Once a negative number is entered stop asking and quit the loop
- Use a while loop to keep asking for a string until a particular string is entered (for example, “bye”). Hint: declare at least two string variables.
Do Not Use
You must use the WHILE statement. You may not use any other looping mechanism.
Sample Interaction / Output
Enter a string to be repeated: coffee How many times to show? 5 coffee coffee coffee coffee coffee 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Enter a number: 119 110 100 90 80 70 60 50 40 30 20 10 Enter a number: 3 3 x 2 = 6 Enter a number: 7 7 x 2 = 14 Enter a number: -2 Enter a string: yo Enter a string: hi Enter a string: hola Enter a string: sup Enter a string: bye
LEGEND
PROGRAM OUTPUT
USER ENTRY
FROM INPUT
CATALOG ID: CPP-LOOP0001
Print Requirements