0% found this document useful (0 votes)
55 views21 pages

Pseudocode Guide 0478 Example

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

Pseudocode Guide 0478 Example

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

Compiled by: Muhammad Ali Ahsan

PSEUDOCODE GUIDE (0478) – Rashaad


Commenting:
//Program made by Ali.

Constant:
A constant is the name of a memory location. Its value does not
change during the execution of a program.

Variable:
A variable is the name of a memory location. Its value may
change during the execution of a program.

Variable Data Types:


INTEGER: Positive and negative whole numbers, E.g: 23, -56.
REAL: Numbers with fractional parts, E.g: 23.54, -56.98.
CHAR: A single letter of the English Language, E.g: A, b.
STRING: A collection of numbers, characters, words and spaces,
E.g: “I study Computer Science.”
BOOLEAN: It can contain only two types of data. E.g: True or
False.

Variable Declaration:
Variable declaration means declaring the data type of the
variable. Variable declaration is done at the start of a program.

Example:
DECLARE num : INTEGER
DECLARE name: STRING
Compiled by: Muhammad Ali Ahsan

Constant Declaration:
CONSTANT pi = 3.14
CONSTANT price = 25

Variable Initialization:
total = 0
count = 0

OUTPUT Statement:
OUTPUT “My name is Ali.”
num = 20
OUTPUT “The number is:”, num

INPUT Statement:
INPUT num
INPUT name

Assignment Statement:
total = 0
total = total + sum
count = count + 1

Task for the Student:


Q. Write pseudocode for a program that inputs the radius of a
circle and calculates the area of that circle. Declare all the
variable and constants that will be used in the program.
Compiled by: Muhammad Ali Ahsan

Conditional Statements:
IF THEN ELSE Statement
CASE Statement

IF THEN ELSE Statement:


Basic Structure:
IF condition1
THEN execution1
ELSE IF condition2
THEN execution2
ELSE IF condition3
THEN execution3
ELSE execution4
ENDIF
ENDIF
ENDIF
Compiled by: Muhammad Ali Ahsan

Example:
Q. Write pseudocode for a program that takes a number as an
input from the user and print whether the entered number is
positive, negative or a zero. Also declare the variables.

Solution:
DECLARE num : INTEGER
OUTPUT “Enter a number:”
INPUT num
IF num > 0
THEN OUTPUT “Positive number.”
ELSE IF num < 0
THEN OUTPUT “Negative number.”
ELSE OUTPUT “Zero”
ENDIF
ENDIF
Compiled by: Muhammad Ali Ahsan

Tasks for the Student:


Q. Write pseudocode for a program that inputs three numbers
from the user and print the highest and the smallest number.

Solution:
INPUT num1
INPUT num2
INPUT num3
IF num1 > num2 AND num1 > num3
THEN highest = num1
ELSE IF num2 > num1 AND num2> num3
THEN highest = num2
ELSE IF num3 > num1 AND num3 > num2
THEN highest = num3
ENDIF
ENDIF
ENDIF
Compiled by: Muhammad Ali Ahsan

CASE Statement:
Basic Structure:
CASE variable OF
condition1 : execution1
condition2 : execution2
condition3 : execution3
OTHERWISE execution4
ENDCASE

Example:
Q. Write pseudocode for a program that takes a number as an
input from the user and print whether the entered number is
positive, negative or a zero. Also declare the variables.

Solution:
OUTPUT “Enter a number:”
INPUT num
CASE num OF
>0 : OUTPUT “Positive number”
<0 : OUTPUT “Negative number”
OTHERWISE OUTPUT “Zero”
ENDCASE

Task for Student:


Q. Write a program to design a calculator that takes two
numbers and a choice from the user. The program should be
able to add, subtract, multiply or divide the entered numbers as
per the choice of the user. Also declare the variables’ data
types.
Compiled by: Muhammad Ali Ahsan

Hint:
IF operation = “+”
THEN answer = num1 + num2
OR
CASE operation OF
“+” : answer = num1 + num2

Task for Student:


Q. Write a program to input the percentage of the student
and print his grade according to the following criteria:
Percentage greater than 90 = A Grade
Percentage greater than 80 and less than equal to 90 = B Grade
Percentage greater than 70 and less than equal to 80 = B Grade
Percentage greater than 60 and less than equal to 70 = B Grade
Percentage less than or equals to 60 means the student has failed.

Hint:
IF per > 80 AND per <= 90
THEN OUTPUT “B Grade”

OR
CASE per OF
> 80 AND <= 90 : OUTPUT “B Grade”

Task for Student:


Q. Write a program to input three numbers from the user and print
which one is the highest.
Compiled by: Muhammad Ali Ahsan

Task for Student:


Q. Write pseudocode that inputs a number from the user and print
whether that number is even or odd using case statement.

Solution:
OUTPUT “Enter a number:”
INPUT num
remainder = MOD(num,2)
CASE remainder OF
0 : OUTPUT “Even number”
OTHERWISE OUTPUT “Odd number”
ENDCASE

Task for Student:


Q. Write pseudocode that inputs the marks of student in a subject and
then calculates his/her percentage. The program should print whether
the student has passed or failed. To pass the exam a student must
obtain a percentage greater than 60. Use CASE statement to write the
solution.

Task for Student:


Q. Write pseudocode for a taxi fare calculation. The program should
input the destination from the user and then print the fare after adding
10$ tax to the fare. The user can choose between the following
destinations:
Location1, $ 100
Location2, $ 200
Location3, $ 300
Location4, $ 400
Location5, $ 500
Compiled by: Muhammad Ali Ahsan

Loops:
FOR Loop (Used when repetitions are known.)
REPEAT UNTIL Loop (Post-Condition Loop) (Used when repetitions are
unknown.)
WHILE Loop (Pre-Condition Loop) (Used when repetitions are
unknown.)

FOR Loop:
FOR variable = 1 to No_of_Repetitions
Body of the loop
NEXT variable

Example:
Q. Write a program to print your name ten times.

Solution
FOR a = 1 TO 10
OUTPUT “Muhammad Ali Ahsan”
NEXT a

Example:
Q. Write a program to input the name of the user and print it as many
times as the user wants.

Solution:
OUTPUT “Enter your name:”
INPUT name
OUTPUT “How many times do you want your name to be printed:”
INPUT No_of_Repetitions
FOR a = 1 TO No_of_Repetitions
OUTPUT “Your name is:”, name
NEXT a
Compiled by: Muhammad Ali Ahsan

Task for Student:


Q. Write pseudocode using FOR Loop to input the name, father’s name,
address and cell number of n user at an office. The program should also
print this data on the screen.

REPEAT UNTIL Loop:


REPEAT
Body of the loop
UNTIL Condition_to_Stop

Example:
REPEAT
OUTPUT “Enter a number or 0 to stop:”
INPUT num
OUTPUT “You entered:”, num
UNTIL num = 0

Example:
a=0
REPEAT
OUTPUT “Muhammad Ali Ahsan”
a=a+1
UNTIL a = 10

Task for Student:


Q. Write pseudocode using REPEAT UNTIL Loop to input the name of
students and print them. The program should stop if the user enters
“stop” as a name.
Compiled by: Muhammad Ali Ahsan

WHILE Loop:
a=0
WHILE a < 10
OUTPUT “Muhammad Ali Ahsan”
a=a+1
ENDWHILE

Task for the Student:


Q. Write pseudocode for a program that inputs the name of the user
and prints it 20 times using FOR Loop, REPEAT UNTIL Loop and WHILE
Loop separately.
Compiled by: Muhammad Ali Ahsan

Q. Write pseudocode for a program that inputs numbers from the user
and print them on the screen.
FOR Loop:
OUTPUT “How many numbers do you want to enter:”
INPUT n
FOR a = 1 TO n
OUTPUT “Enter a number:”
INPUT num
OUTPUT “You entered:”, num
NEXT a

REPEAT UNTIL Loop:


REPEAT
OUTPUT “Enter a number or 0 to stop:”
INPUT num
OUTPUT “You entered:”, num
UNTIL num = 0

WHILE Loop:
num = 1
WHILE num <> 0
OUTPUT “Enter a number or 0 to stop:”
INPUT num
OUTPUT “You entered:”, num
ENDWHILE
Compiled by: Muhammad Ali Ahsan

Task for the Student:


Q. Write pseudocode for program to input the names from the user
and print them on the screen. Use FOR Loop, REPEAT UNTIL Loop and
WHILE Loop separately.

REPEAT UNTIL Loop:


REPEAT
OUTPUT “Enter a name or stop to end:”
INPUT name
OUTPUT “You entered:”, name
UNTIL name = “stop”

WHILE Loop:
name = “abc”
WHILE name <> “stop”
OUTPUT “Enter a name or stop to end:”
INPUT name
OUTPUT “You entered:”, name
ENDWHILE
Compiled by: Muhammad Ali Ahsan

Conditions in Loops:
Q. Write pseudocode for a program to input numbers from the user
and print whether the number is positive or negative.

FOR Loop:
OUTPUT “How many numbers do you want to enter:”
INPUT n
FOR a = 1 TO n
OUTPUT “Enter a number:”
INPUT num
IF num > 0
THEN OUTPUT “Positive”
ELSE IF num < 0
THEN OUTPUT “Negative”
ENDUF
ENDIF
NEXT a

REPEAT UNTIL Loop:


REPEAT
OUTPUT “Enter a number or 0 to stop:”
INPUT num
IF num > 0
THEN OUTPUT “Positive”
ELSE IF num < 0
THEN OUTPUT “Negative”
ENDIF
ENDIF
UNTIL num = 0
Compiled by: Muhammad Ali Ahsan

Task for the Student:


Q. Write pseudocode for a program that input the marks of students in
a subject and print whether each student has passed or failed. Passing
marks are 50. Use REPEAT UNTIL and WHILE Loops to write the solution
separately.

Totaling:
total = 0
total = total + num

Q. Write a program to input numbers from the user and find their
total.

FOR Loop:
total = 0
OUTPUT “How many numbers do you want to enter:”
INPUT n
FOR a = 1 TO n
OUTPUT “Enter a number:”
INPUT num
total = total + num
NEXT a
OUTPUT “The total of the entered numbers is:”, total

Task for the Student:


Q. Write a program to input numbers from the user and find their total
using WHILE Loop.

Q. Write a program to input 40 numbers from the user and find their
average. Complete the program using FOR Loop.

Hint: average = total /count


Compiled by: Muhammad Ali Ahsan

Counting:
count = 0
count = count + 1

Q. Write a program to input 50 numbers from the user and count how
many of them and positive and negative.

Solution:
pos = 0
neg = 0
FOR a = 1 TO 50
INPUT num
IF num > 0
THEN pos = pos + 1
ELSE IF num < 0
THEN neg = neg + 1
ENDIF
ENDIF
NEXT a
OUTPUT “Positive numbers are:”, pos
OUTPUT “Negative numbers are:”, neg
Compiled by: Muhammad Ali Ahsan

Even and Odd Numbers:


Q. Write pseudocode for a program to input 20 numbers from the user
and count how many numbers are even and odd.

Solution (FOR Loop):


even = 0
odd = 0
FOR a = 1 TO 20
OUTPUT “Enter a number:”
INPUT num
IF MOD(num, 2) = 0
THEN even = even + 1
ELSE IF MOD(num, 2) <> 0
THEN odd = odd + 1
ENDIF
ENDIF
NEXT a
OUTPUT “Even numbers entered are:”, even
OUTPUT “Odd numbers entered are:”, odd

Task for the Student:


Q. Write pseudocode for a program to input 20 numbers from the user
and count how many numbers are even and odd using REPEAT UNTIL
Loop.
Compiled by: Muhammad Ali Ahsan

Highest and Smallest Numbers:


Q. Write a program to input numbers from the user and print the
highest and the smallest number entered by the user.

Solution:
OUTPUT “How many numbers do you want to enter:”
INPUT n
OUTPUT “Enter a number:”
INPUT num
highest = num
smallest = num
FOR a = 1 TO n-1
OUTPUT “Enter a number:”
INPUT num
IF num > highest
THEN highest = num
ENDIF
IF num < smallest
THEN smallest = num
ENDIF
NEXT a
OUTPUT “The highest number is:”, highest
OUTPUT “The smallest number is:”, smallest
Compiled by: Muhammad Ali Ahsan

Task for the Student:


Q. Write a program to input numbers from the user and print the
highest and the smallest number using WHILE Loop.

Solution-1:
num = 0
OUTPUT “Enter a number:”
INPUT num
highest = num
smallest = num
WHILE num <> -9876
OUTPUT “Enter a number -9876 or to stop:”
INPUT num
IF num > highest
THEN highest = num
ENDIF
IF num < smallest
THEN smallest = num
ENDIF
ENDWHILE
Compiled by: Muhammad Ali Ahsan

Solution-2:
continue = “start”
OUTPUT “Enter a number:”
INPUT num
highest = num
smallest = num
WHILE continue <> “stop”
OUTPUT “Enter a number:”
INPUT num
IF num > highest
THEN highest = num
ENDIF
IF num < smallest
THEN smallest = num
ENDIF
OUTPUT “Enter any text to continue or stop to end the program:”
INPUT continue
ENDWHILE

Task for the Student:


Q. Write a program to input numbers from the user and print the
highest and the smallest number using REPEAT UNTIL Loop. The
program should also print the average of the positive numbers entered
by the user.

Tasks for the Student:


Q. Write pseudocode for a program that inputs numbers from the user
and find their total and average. The program should also count how
many numbers are positive, negative, even and odd. The program
should also print the highest and the smallest number entered by the
user.
Compiled by: Muhammad Ali Ahsan

Q. Write a program to calculate the average daily and weekly


temperature. The program should input 10 readings of temperature
each day.

Hint:
FOR a = 1 TO 7
FOR b = 1 TO 10
INPUT temp
NEXT b
NEXT a

You might also like