NOTE: There are no TABS on the codes. THESE ARE MY PERSONAL solutions to the problem. Copy at your own risk.
1. Design a ‘While’ loop that lets the user enter a number. The number should be multiplied by 10, and the result stored in a variable named ‘product’. The loop should iterate as long as ‘product’ contains a value less than 100.
ANSWER:
Module main()
Declare Integer product = 0
Declare Integer num = 0
While product < 100
Display "Enter a number: "
Input num
product = num * 10
Display "Product: " , product
End While
End Module
2. Design a 'Do-While' loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat; otherwise it should terminate.
ANSWER:
Module main()
Declare Integer num1 = 0
Declare Integer num2 = 0
Declare Integer sum = 0
Declare String response
Do
Display "Enter first number: "
Input num1
Display "Enter second number: "
Input num2
sum = num1 + num2
Display "The sum is: " , sum
Display "Enter Y to continue: "
Input response
While response == "Y"
End Module
3. Design a 'For' loop that displays the following set of numbers:
0, 10, 20, 30, 40, 50... 1000
ANSWER:
Module main()
Declare Integer counter = 0
For counter = 0 To 1000 Step 10
Display counter , " "
End For
End Module
4. Design a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered.
ANSWER:
Module main()
Declare Integer counter = 0
Declare Integer num = 0
Declare Integer total = 0
For counter = 0 To 10
Display "Enter #: "
Input num
total = total + num
End For
Display "Total: " , total
End Module
5. Design a 'For' loop that calculates the total of the following series of numbers:
1/30 + 2/29 + 3/28 + ...30/1
ANSWER:
Module main()
Declare Integer num = 1
Declare Integer den = 30
Declare Integer total = 0
For num = 1 To 30
total = total + num/den
den = den -1
End For
Display "Total: " , total
End Module
6. Design a nested loop that displays 10 rows of # characters. There should be 15 # characters in each row.
ANSWER:
Declare Integer rowCount = 0
Declare Integer itemCount = 0
Declare String line
For rowCount = 0 TO 10
For itemCount = 0 To 15
line = line , "#"
End For
Display line
line = " "
End For
7. Convert the 'While' loop in the following code to a 'Do-While' loop:
Declare Integer x = 1
While x > 0
Display “Enter a number.”
Input x
End While
ANSWER:
Declare Integer x = 1
Do
Display “Enter a number.”
Input x
While x > 0
8. Convert the ‘Do-While’ loop in the following code to a ‘While’ loop:
Declare String sure
Do
Display “Are you sure you want to quit?”
Input sure
While sure != “Y” AND sure != “y”
ANSWER:
Declare String sure
Display “Are you sure you want to quit?”
Input sure
While sure != “Y” AND sure != “y”
Display “Are you sure you want to quit?”
Input sure
End While
9. Convert the following ‘While’ loop to a ‘For’ loop:
Declare Integer count = 0
While count < 50
Display "The count is " , count
Set count = count + 1
End While
ANSWER:
Declare Integer count = 0
For count = 0 To 50
Display "The count is " , count
End For
10. Convert the following 'For' loop to a 'While' loop
Declare Integer count
For count = 0 To 50
Display count
End For
ANSWER:
Declare Integer count = 0
While count < 50
Display count
Set count = count + 1
End While