0% found this document useful (0 votes)
16 views7 pages

Pseudocode Examples for Basic Programming Tasks

Uploaded by

evamariabijo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Pseudocode Examples for Basic Programming Tasks

Uploaded by

evamariabijo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. A program needs to display the numbers from 1 to 100.

Write a pseudocode

using a FOR loop to achieve this.

2. Create the pseudocode that calculates the average of a series of numbers

entered by a user.

3. Write a pseudocode to calculate the total sum of all even numbers between 2

and 10 (inclusive). Use a FOR loop with a STEP value.

4. Write a pseudocode FOR loop to calculate the sum of all odd numbers between 1

and 20 (inclusive). The final sum should be stored in a variable named Total.

5. A program is required to analyze a set of numbers entered by a user and count

how many are positive, how many are negative, and how many are zero.

Write the pseudocode for a program that:

1. Asks the user for the total number of values they will enter.
2. Uses a FOR loop to get each number individually.
3. Inside the loop, uses a nested IF...THEN...ELSE structure to categorize each number and
add to its respective counter.
o Check if the number is greater than zero.
o If not, check if it is less than zero.
o Otherwise, it must be zero.
4. After the loop, display the final count for positive, negative, and zero numbers.
Qn

A program is needed to repeatedly ask a user for a password until they enter the correct
one. The correct password is "secret". The program should stop as soon as the correct
password is entered.

Write pseudocode to solve this problem.

Q2

A program is required to calculate the sum of a list of numbers. The user doesn't know
in advance how many numbers they will enter, but they will enter the number -1 to
indicate that they are finished. This number, -1, should not be included in the sum.

Write pseudocode to solve this problem.

Q3.

A program needs to count the number of digits in a positive integer entered by a user.
The number of digits can be found by repeatedly dividing the number by 10 until it
becomes 0.

Write pseudocode to solve this problem.

Syntax INT

Q4.

calculates the sum of many numbers

Initialize variables for the current total and the flag (to end the program).

Start a while loop which checks whether the flag is true or not

Ask the user for a number.

Add the number to the current total.

Ask the user if they wish to exit.

If the answer to is “no”, then go back to loop

Else, set the flag to false.


Output the final total.

Answer 1:
DECLARE password : STRING

OUTPUT "Enter the password:"


INPUT password

WHILE password <> "secret"


OUTPUT "Incorrect password. Please try again:"
INPUT password
ENDWHILE

OUTPUT "Access granted."

Question 2: Summing Numbers Until a Sentinel Value

Answer 2:
DECLARE number : INTEGER
DECLARE sum : INTEGER

SET sum = 0

OUTPUT "Enter a number (enter -1 to stop):"


INPUT number

WHILE number <> -1


sum <- sum + number
OUTPUT "Enter a number (enter -1 to stop):"
INPUT number
ENDWHILE

OUTPUT "The sum of the numbers is: " & sum

Question 3: Counting Digits in a Number

Answer 3:
DECLARE number : INTEGER
DECLARE count : INTEGER

SET count = 0

OUTPUT "Enter a positive integer:"


INPUT number

IF number = 0 THEN
SET count = 1
ELSE
WHILE number > 0
number <- INT(number / 10)
count <- count + 1
ENDWHILE
ENDIF

OUTPUT "The number has " & count & " digits."

In the context of the CIE 9618 pseudocode standard, it is not explicitly necessary to declare the counter

variable before the FOR loop. The loop construct itself often handles the declaration implicitly.
Answer:

FOR count ← 1 TO 5
OUTPUT count
NEXT count

Question 2

Question:

Answer:

DECLARE totalSum : INTEGER


totalSum ← 0
FOR num ← 2 TO 10 STEP 2
totalSum ← totalSum + num
NEXT num
OUTPUT "The sum is: ", totalSum

Write a pseudocode FOR loop that iterates from 1 to 50 and outputs each number.

Answer:

FOR count ← 1 TO 50
OUTPUT count
NEXT count

Question 2

Write a pseudocode FOR loop to calculate the sum of all odd numbers between 1 and 20
(inclusive). The final sum should be stored in a variable named Total.

Answer:

DECLARE Total : INTEGER


Total ← 0
FOR num ← 1 TO 20 STEP 2
Total ← Total + num
NEXT num

Question 3
A program needs to display the first 10 multiples of 5, starting from 5. Write a pseudocode FOR
loop to accomplish this.

Answer:

FOR i ← 1 TO 10
OUTPUT i * 5
NEXT i

You might also like