Professional Documents
Culture Documents
Instruction: Complete a defining diagram and write a pseudocode algorithm to solve each of
the following:
1. Write a pseudocode algorithm to read three numbers and find their products and
average and print the sum, average and product.
Alt Alt
Alt
Three numbers Calculate sum by adding Sum
all three numbers Average
Product
Calculate average by
dividing sum by three
Calculate product by
multiplying all three
numbers
BEGIN
Avg ← sum/3
Prod ← num1*num2*num3
ENDq
2. Write a pseudocode algorithm to accept the current year and the year a person was
born. Calculate and print the age of the person.
BEGIN
PRINT “Please enter the current year and your birth year”
READ current_year, birth_year
Age ← current_year - birth_year
PRINT “You are”, Age, “years old”
END
3. In groups, create a problem statement for any given problem. Write a pseudocode algorithm
to solve the problem.
IF Statements
1. A company gives out bonuses based on the amount of income generated by their sales
representatives per month. Once the income is greater than $50,000.00, a bonus of 10%
of the generated income is given to the employees. Read the income generated and
print the bonus.
BEGIN
CONSTANT
BonusPercent = 0.1
DECLARE
READ income
ENDIF
END
2. A company gives out bonuses based on the amount of income generated by their sales
representatives per month. Once the income is greater than $50,000.00, a bonus of 10%
of the generated income is payable; otherwise the bonus is 3% of the generated income.
Read the income generated and print the bonus.
BEGIN
CONSTANT
BonusPercent_1 = 0.1
BonusPercent_2 = 0.03
DECLARE
READ Sr_income
ELSE
ENDIF
END
3. Write a pseudocode algorithm to accept the name of a student, subject, score on test,
and the amount the test was marked out of. Calculate the percentage grade the student
received. The algorithm should also determine the letter grade based on the following:
BEGIN
DECLARE
Student_name, subject AS STRING
percentage_grade AS REAL
Total_marks, Test_score AS INTEGER
Letter_grade AS CHAR
PRINT “Please enter Student name, subject, Test score, Total marks”
READ Student_name, subject, Test_score, Total_marks
END
Loops
Categories:
FOR Construct
Statements to be repeated
ENDFOR
Example
FOR counter ← 1 TO 20 DO
ENDFOR
Practice Question
(1) Write an algorithm to read ELEVEN numbers, find their average and print it.
The algorithm should also print the number of times the number 6 occurs in
the data. For example, given the input data:
4 6 9 6 5 6 10 7 0 16
The algorithm should print 7 as the average and 3 as the number of times 6
occurs.
BEGIN
Declare
Num1, num2, num3, sum, product, difference As integer
Average As real
Choice As string
Print “Enter a choice you wish to execute: sum, avg, prod or diff”
Read choice
Else
Else
Else
Else
ENDIF
ENDIF
ENDIF
END
Pascal - WHILE - DO
WHILE condition DO
BEGIN
Statements;
END; // end of while loop
WHILE condition DO
Statement; // end of while loop
REPEAT
Statements;
UNTIL Condition;
BEGIN
DECLARE
QUANTITY AS INTEGER
Res AS Char
Res ← ‘Y’
PRINT “please enter the quantity and the unit price of the product”
READ QUANTITY
READ UNIT_PRICE
COST ← QUANTITY*UNIT_PRICE
READ res
ENDWHILE
END
BEGIN
CONSTANT
VIP= 5000
Regular = 2000
DECLARE
Num_of_reg_tickets ← 0
Num_of_vip_tickets ← 0
Res ← “Y”
REPEAT
READ Age
REPEAT
READ Ticket
ELSE
ELSE
ENDIF
ENDIF
READ Res
ENDWHILE
END
Arrays - Pascal
Declaration
VAR
BEGIN
PRODUCT← 1
FOR X ← 1 TO 10 DO
PRINT “Enter a number”
READ num[X]
SUM ← SUM + num[X]
PRODUCT ← PRODUCT*num[X]
ENDFOR
AVG← SUM/10
PRINT “the sum is”, SUM
PRINT “the product is”, PRODUCT
PRINT “the average is”, AVG
PRINT “The numbers entered are:”
FOR X ← 1 TO 10 DO
PRINT num[x]
ENDFOR
END
Q. Write a program to accept the names and ages of 10 patients to see a pediatrician. Only
children from 0 to 17 are allowed to see the doctor. If the user enters an age outside of this
range, the program should force the user to enter another age. In the end, the program should
print the average age of the children and all the names and ages entered by the user.
FOR x ← 1 to 10 DO
Print “enter a name and age of a person”
Read name[x], age[x]
ENDFOR
PROCEDURES - A distinct portion of a program that
carries out a set of tasks and returns control to the main part
of the program.
Outline
Procedure name;
Var
Local variables (variable that can only be used in this
section of the program)
BEGIN
Statements;
Procedure Welcome_Screen;
BEGIN
Writeln (‘********************Welcome*************************’);
END;
Procedure Get_data
Declare
X As integer
BEGIN
SUM ← 0
PRODUCT← 1
FOR X ← 1 TO 10 DO
PRINT “Enter a number”
READ num[X]
SUM ← SUM + num[X]
PRODUCT ← PRODUCT*num[X]
ENDFOR
AVG← SUM/10
END
PROCEDURE display_Results;
BEGIN
PRINT “the sum is”, SUM
PRINT “the product is”, PRODUCT
PRINT “the average is”, AVG
END
PROCEDURE DisplayALLNum
BEGIN
PRINT “The numbers entered are:”
FOR i ← 1 TO 10 DO
PRINT num[i]
ENDFOR
END
BEGIN
DECLARE
Array num[10] as Integer
SUM, product, i as INTEGER
AVG as REAL
Welcome_Screen;
Get_Data;
Display_results;
DisplayALLNum;
END.