11.2 Constructs (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Consider a scenario where you need to calculate the sum of the first 'n' positive integers. Write pseudo-code for a count-controlled loop to achieve this. Assume 'n' is an integer input from the user.
Pseudo-code:
- Start
- Get input value for n
- Set sum to 0
- Set i to 1
- While i ≤ n do:
- sum = sum + i
- Increment i by 1 (i = i + 1)
- Print sum
- End
2.
Write pseudo-code for a 'while' loop that repeatedly prompts the user for positive integer input until a valid input is provided. The loop should continue until the user enters a number greater than 0. The prompt message should be "Enter a positive integer:". The user's input should be stored in a variable called number
.
Pseudo-code:
- Start
- Set
number
to 0 - while
number
do: - Display the message "Enter a positive integer: "
- Read the user's input and store it in
number
- if
number
then: - Display the message "Invalid input. Please enter a positive integer."
- endif
- endwhile
- Display the value of
number
- Stop
3.
Write pseudo-code for a 'pre-condition' loop that repeatedly prompts the user for positive integer input until a number greater than 10 is entered. The loop should include input validation to ensure the user enters a valid integer.
Pseudo-code:
- Start
- Display a message prompting the user to enter a positive integer (greater than 10).
- Get input from the user and store it in a variable number.
- While number is not a positive integer greater than 10:
- Display an error message indicating the input is invalid (e.g., "Please enter a number greater than 10").
- Prompt the user to enter a positive integer again.
- End While
- Display a message confirming the valid number entered (e.g., "You entered the number: number").
- End