Pseudocode Guide 0478 Example
Pseudocode Guide 0478 Example
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 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
Conditional Statements:
IF THEN ELSE Statement
CASE Statement
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
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
Hint:
IF operation = “+”
THEN answer = num1 + num2
OR
CASE operation OF
“+” : answer = num1 + num2
Hint:
IF per > 80 AND per <= 90
THEN OUTPUT “B Grade”
OR
CASE per OF
> 80 AND <= 90 : OUTPUT “B Grade”
Solution:
OUTPUT “Enter a number:”
INPUT num
remainder = MOD(num,2)
CASE remainder OF
0 : OUTPUT “Even number”
OTHERWISE OUTPUT “Odd number”
ENDCASE
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
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
WHILE Loop:
a=0
WHILE a < 10
OUTPUT “Muhammad Ali Ahsan”
a=a+1
ENDWHILE
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
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
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
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
Q. Write a program to input 40 numbers from the user and find their
average. Complete the program using FOR Loop.
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
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
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
Hint:
FOR a = 1 TO 7
FOR b = 1 TO 10
INPUT temp
NEXT b
NEXT a