topic badge

12.10 Loops

Worksheet
Loops
1

Consider the following pseudocode:

a

Find the initial value of a.

b

Find the final value of a.

c

Find the value of \text{OUTPUT}.

\text{START} \\ \text{SET } a = 9 \\ \text{SET sum = 0} \\ \text{WHILE } a \gt 3\\ \quad \text{sum = sum} + a\\ \quad a = a - 1 \\ \text{END WHILE} \\ \text{PRINT sum} \\ \text{END}

2

For each of the following pseudocodes:

i

Find the initial value of a.

ii

Find the \text{OUTPUT} of the code.

a

\text{START} \\ \text{SET } a = 2 \\ \text{SET sum} = 0 \\ \text{WHILE a < 9} \\ \quad \text{sum = sum} + a \\ \quad a = a + 1 \\ \text{END WHILE} \\ \text{PRINT sum} \\ \text{END}

b

\text{START} \\ \text{SET } a = 3 \\ \text{SET sum} = 0 \\ \text{SET } n = 0 \\ \text{WHILE } a \lt 12 \\ \quad \text{sum = sum + a} \\ \quad a = a + 1 \\ \quad n = n + 1 \\ \text{END WHILE} \\ \text{PRINT sum / } n \\ \text{END}

3

Find the final value, n, for the following pseudocode:

\text{START} \\ \text{SET } a = 4 \\ \text{SET } n = 0 \\ \text{WHILE } a \gt 1 \\ \quad \text{sum = sum + a} \\ \quad a = a \, / \, 2 \\ \quad n = n + 1 \\ \text{END WHILE} \\ \text{END}

4

Determine the value that is printed at the end of the following pseudocodes:

a

\text{START} \\ \text{SET } a = 19 \\ \text{WHILE } a \gt 5 \\ \quad a = a - 1 \\ \text{END WHILE} \\ \text{PRINT } a \\ \text{END}

b

\text{START} \\ \text{SET } a = 1 \\ \text{WHILE } a \lt 24\\ \quad \text{PRINT } a \\ \quad a = a * 2 \\ \text{END WHILE} \\ \text{END}

c

\text{START} \\ \text{SET } a = 5 \\ \text{WHILE } a \lt 7 \\ \quad \text{PRINT } a \\ \quad a = a + 1 \\ \text{END WHILE} \\ \text{END}

d

\text{START} \\ \text{SET } a = 2 \\ \text{WHILE } a \lt 10 \\ \quad \text{PRINT } a \\ \quad a = a + 2 \\ \text{END WHILE} \\ \text{END}

e

\text{START} \\ \text{SET } a = 5 \\ \text{WHILE } a \lt 20 \\ \quad \text{PRINT } a \\ \quad a = a * 2 \\ \text{END WHILE} \\ \text{END}

5

Consider the algorithm represented by the flow chart:

Arrange the following lines of pseudocode in the correct order for the algorithm.

StepCode
A\text{WHILE } a \gt 4
B\text{END WHILE}
C\text{PRINT } a
D\text{SET } a = 4
Ea = a - 1
6

Consider the algorithm represented by the flow chart:

Arrange the following lines of pseudocode in the correct order for the algorithm.

StepCode
Aa = a + 1
B\text{SET } a = 4
C\text{WHILE } a \neq 5
D\text{PRINT sum}
E\text{sum = sum} + a
F\text{END WHILE}
G\text{SET sum}=0
7

Consider the algorithm for finding the sum of the first n natural numbers represented by the flow chart.

a

Find the output of this algorithm if n=4.

b

Arrange the following lines of pseudocode for adding the first 50 natural numbers in the correct order for the algorithm.

StepCode
A\text{sum = } a + \text{sum}
B\text{PRINT sum}
C\text{SET } a=1
Da=a+1
E\text{GO TO LINE } 3
F\text{SET sum}=0
G\text{IF } a=50 \text{ THEN GO TO LINE } 7
8

The factorial of a non-negative integer n is given by n! = n \times (n-1) \times (n-2) \times .... \times 2 \times 1. For example, 5! = 5 \times 4 \times 3 \times 2 \times 1=120.

Consider the algorithm for finding n! represented by the flow chart:

a

Find the output of this algorithm if n=6.

b

Arrange the following lines of pseudocode in the correct order for finding 50!.

StepCode
Ap = p * a
Ba = a + 1
C\text{PRINT } p
D\text{SET } a = 1
E\text{IF } a=50 \text{ THEN GO TO LINE } 7
F\text{SET } p=1
G\text{GO TO LINE } 3
Sign up to access Worksheet
Get full access to our content with a Mathspace account

What is Mathspace

About Mathspace