Algorithm
Non-Iterative
Algorithm
Flow Chart
(Simple Input-
Pse
In non-iterative algorithm we usually accept/input data to find/calculate some values either
through some formula or from simple calculation. Once calculation is done we release the
results. In some questions your calculation might be conditional, some time your output
would be depending on some criteria but in any case there wont be any loop involved.
NonIterative
Iterative
Write an algorithm in the form of a flowchart which takes temperatures input in Fahrenheit. Convert the
temperature in Celsius with the help of following formula:
Formula/Value
Calculation
Temp (in Celsius)=(Temp
(in Fahrenheit)
- 32)/1.8
Count based
Loop
and output temperature in Celsius.
Formula/Val
ue
Calculation
Start
Start of Flowchart
Totaling
Counting
Here we decide the variable to be used and initialized them. In this particular question we have taken Temp
TempA0
TempB0
Dry running of flowcharts
Initialzation
TempA
TempB
Input
0
0
TempA
Input
Output
TempB
TempA
TempB
37=(100100
37
32)/1.8
Input Command
Process Box
TempB (TempA-32)/1.8
Output Command
Output
TempB
End of Flowchart
End
End
Practice question:
Question 01:
Customers can withdraw cash from an Automatic Teller Machine (ATM).
withdrawal is refused if amount entered > current balance
withdrawal is refused if amount entered > daily limit
if current balance < $100, then a charge of 2% is made
if current balance $100, no charge is made
Write an algorithm which inputs a request for a sum of money, decides if a withdrawal can be
made and calculates any charges. Appropriate output messages should be included.
Input Amount
If Amount>Current Balance then Print Withdrawal Refused
If Amount>Daily Limit then Print Withdrawal Refused
Current Balance= Current Balance Amount
If Current Balance<100 then Charges=Current Balance -2% Else Charges=0
Reason
Sum of Money
Current
Balance
Daily Limit
Variable Name
Amount
CB/Balance
Type
Number
Number
Initial Value
0
0
DL/Limit/DailyLi
Number
0
mit
Question 02:
Charges
Ch/Charges
Number
0
Regis lives in Brazil and
often travels to USA, Europe and Japan. He wants to be able to convert Brazilian Reais into US
dollars, European euros and Japanese yen. The conversion formula is:
currency value = number of Reais X conversion rate
For example, if Regis is going to USA and wants to take 1000 Reais (and the exchange rate is
0.48) then he would input USA, 1000 and 0.48 and the output would be: 480 US dollars.
Write an algorithm, using pseudocode, which inputs the country he is visiting, the exchange
rate and the amount in Brazilian Reais he is taking. The output will be value in foreign
currency and the name of the currency.
Question 03:
Daniel lives in Italy and travels to Mexico, India and New Zealand. The times differences are:
Country
Mexico
India
New Zealand
Hours
-7
+4
+11
Minutes
0
+30
0
Thus, if it is 10:15 in Italy it will be 14:45 in India.
(a) Write an algorithm, using pseudocode or otherwise, which:
Inputs the name of the country
Inputs the time in Italy in hours (H) and minutes (M)
Calculates the time in the country input using the data from the table
Outputs the country and the time in hours and minutes
(b) Describe, with examples, two sets of test data you would use to test your algorithm.
Iterative Algorithm
(Simple Input-
In iterative algorithm you repeat one or more statement many times. Some time you know
how many times the process has to be repeated. This is so called count based loop/iteration
i.e. calculate BMI of 30 students or calculate tax of 5000 houses. But on many occasions
you dont know how many times you have to repeat the process. In such cases repetition
will be depending on some condition i.e. repeat while a certain condition is True i.e. inputs
a set of positive numbers (which end with -1) & outputs the value of the largest (highest)
number input. In this question you dont know how many positive numbers are there rather
you have been given a condition i.e. enter -1 to stop or exit the loop.
In iterative algorithm we usually do:
1.
2.
3.
4.
Value calculation (either through any formula or by simple calculation)
Counting (counting a sub-set data based on some condition)
Totaling (usually done to calculate average)
Finding extreme values (to find single largest/smallest, highest/lowest/,
maximum/minimum value)
Start
Start
Write an algorithm, using pseudocode or a
flowchart, which
Num0
inputs 50 positive numbers
Count0
outputs how many of the numbers
CountA0
were > 100
Largest00
outputs the value of the largest
Yes
Count=50
(highest) number input
Write an algorithm, using pseudocode or a
Num0
flowchart, which
CountA0
inputs a set of positive numbers
Largest0
(which end with -1)
Input
outputs how many of the numbers
Num
were > 100
outputs the value of the largest
Yes
Num= - 1
(highest) number
input
No
No
Input
Num
Write an algorithm, using pseudocode or a
Yes
Num>100
flowchart, which
inputs 50 positive numbers
outputs the average (mean) value of
the input numbers
CountA CountA + 1
outputs the value of the largest
(highest) number input
Yes
Num>Largest
Yes
Num>100
No
No
CountA CountA + 1
Yes
Num>Largest
No
No
Largest Num
Largest Num
Count Count + 1
Print CountA , Largest
End
Input
Num
Print CountA , Largest
End
Write an algorithm, using pseudocode or a
flowchart, which
inputs a set of positive numbers
(which end with -1)
outputs the average (mean) value of
the input numbers
outputs the value of the largest
Start
(highest) number input
Start
Count0
Total0
Largest00
Num0
Avg0
Yes
Num0
Total0
Count0
Largest00
Avg0
Count=50
No
Input
Num
Input
Num
Total Total + Num
Yes
Yes
Num>Largest
Num= - 1
No
No
TotalTotal + Num
Largest Num
CountCount + 1
Count Count + 1
Avg Total / 50
Print Avg , Largest
Yes
Num>Largest
No
Largest Num
Input
Num
End
Avg Total / Count
Print Avg , Largest
End
1) Start of Flowchart
Start
Count0
CountA0
and
initialized
CountB0
TempA0
TempB0
Total0
Avg0
Highest0
Lowest1000
2) Here we decide the variables to be used
them. All numerical variable will b
11) Always Calculate Average outside loop.
3) Start of the
loop. Set loop exit condition i.e. Count=100 if you ha
Yes
is
Count=100
4) Input Command
No
AvgTotal/100
Input
TempA
5) Value calculation if required.
Output
Avg, CountA, CountB Highest, Lowest
TempB (TempA-32)/1.8
6) Output result only if point 5 exists.
13) End of Flowchart
Output
TempB
End
7) Add the input value or result of value calculated if average
Total Total +TempB
12) Always Print Average, Counting Variables, and Extreme Values outside the loop.
8) Set the counting condition if tally is required and increment the
No
Yes
CountA CountA+1
Question: Write an algorithm in
the form of a flowchart which
takes temperatures input in
Fahrenheit over a 100 day period
(once per day). Convert the
temperature in Celsius with the
help of following formula:
Temp (in Celsius)=(Temp
(in Fahrenheit) - 32)/1.8
and output temperature in
Celsius, also print:
(b) number of days when the
temperature was below 20C
and the number of days when
the temperature was 20C and
above.
(c) average temperature of 100
days.
(d) the highest and lowest
temperature recorded in these
100days.
is
TempB<20
?
CountB CountB+1
9) If extreme values such as Highest/Lowest, Tallest/Shortest, M
Yes
No
is
TempB>Highest
?
Highest TempB
Yes
No
Lowest TempB
is
TempB<Lowest
?
10) Increment the loop counter by 1 for the next iteration and
Count Count+1
1) Start of Flowchart
Start
Count0
CountA0
and
initialized
CountB0
TempA0
TempB0
Total0
Avg0
Highest0
Lowest1000
Day
2) Here we decide the variables to be used
them. All numerical variable will b
3) Input Command
Input Day,
TempA
12) Always Calculate Average outside loop.
Friday
4) Start
of theDay=
loop.
Use input variable to set loop exit conditio
Yes
5) Value calculation if required.
No
AvgTotal/Count
TempB (TempA-32)/1.8
6) Output result only if point 5 exists.
Output
Avg, CountA, CountB Highest, Lowest
Output
TempB
7) Add the input value or result of value calculated if averag
Total Total +TempB
14) End of Flowchart
End
8) Nothing to do with loop but required for com
Count Count+1
13) Always Print Average, Counting Variables, and Extreme Values outside the loop.
9) Set the counting condition if tally is required and increment the
is
TempB<20
?
Yes
CountA CountA+1
Question: Write an algorithm in
the form of a flowchart which
takes temperatures input in
Fahrenheit on certain days (stop
by Friday). Convert the
temperature in Celsius with the
help of following formula:
Temp (in Celsius)=(Temp
(in Fahrenheit) - 32)/1.8
and output temperature in
Celsius, also print:
(b) number of days when the
temperature was below 20C
and the number of days when
the temperature was 20C and
above.
(c) average temperature of 100
days.
(d) the highest and lowest
temperature recorded in these
100days.
Yes
No
CountB CountB+1
is
TempB>Highest
?
No
Highest TempB
10) If extreme values such as Highest/Lowest, Tallest/Shortest,
Yes
is
TempB<Lowest
?
No
Lowest TempB
11) Input command for next iteration before re
Input Day,
TempA
Value Calculation
In many questions you will be asked to calculate some values with the help of any given
formula or through simple calculation. Sometime this calculation is quite simple (for
example convert temperature from Fahrenheit to Celsius) but on many occasions it will be
conditional (calculate tax based on their house value). In any case once you find the value
you have to release the result. Dont mix concept of totaling, counting, average etc with
value calculation.
Simple Calculation/Simple Output:
Start
Count0
TempA0
TempB0
Yes
is
Count=100
?
No
Input
TempA
TempB (TempA-32)/1.8
Output
TempB
CountCount+1
End
End
Write an algorithm in the form of a flowchart which
takes temperatures input in Fahrenheit over a 100
day period (once per day). Convert the temperature
in Celsius with the help of following formula:
Temp (in Celsius)=(Temp (in Fahrenheit) - 32)/1.8
and output temperature in Celsius
Value Calculation
Simple Calculation but Conditional Output:
Start
Count0
TempA0
TempB0
Yes
Write an algorithm in the form of a flowchart which takes
temperatures input in Fahrenheit over a 100 day period (once per
day). Convert the temperature in Celsius with the help of
following formula: Temp (in Celsius)=(Temp (in Fahrenheit) 32)/1.8
and output temperature in Celsius with this comment.
If temperature exceeds 30C then comment Hot summer day
else print Normal temperature.
is
Count=100
?
No
Input
TempA
TempB (TempA-32)/1.8
Yes
is
TempB>30
?
Output
TempB, Hot Summer Day
Output
TempB, Normal Temperature
CountCount+1
End
End
No
Value Calculation
Conditional Calculation with Simple Output:
Write an algorithm in the form of a flowchart which takes temperatures with scale input over a 100 day
period (once per day). Convert the temperature in Celsius if input temperature is in Fahrenheit with the
help of following formula:
Temp (in Celsius)=(Temp (in Fahrenheit) - 32)/1.8
And in Fahrenheit if input Temperature is in Celsius. and output converted temperature.
Temp (in Fahrenheit)=(Temp (in Celsius) + 32) x1.8
Start
Count0
TempA0
TempB0
Yes
is
Count=100
?
No
Input
TempA, Scale
Yes
is
Scale= Fahrenheit
?
TempB (TempA+32)x1.8
No
TempB
Output
TempB
CountCount+1
End
(TempA-32)/1.8
A town contains 5000 houses. Each house owner must pay tax based on the value of the
house. Houses over $200 000 pay 2% of their value in tax, houses over $100 000 pay 1.5% of
their value in tax and houses over $50 000 pay 1% of their value in tax. All others pay no tax.
Write an algorithm to solve this problem in the form of a flowchart.
Start
Count0
HValue0
Tax0
Yes
Count=5000
No
Input HValue
Yes
HValue>200
000
No
Tax (HValue * 2) /
100
Yes
HValue>100000 and
HValue<200000
No
Tax (HValue *
1.5) / 100
Yes
HValue>50000 and
HValue<100000
Tax (HValue * 1) /
100
Yes
HValue<500
00
Tax 0
Print Tax
Count Count + 1
End
No
No
Practice Question:
Question 01:
A formula for calculating the body mass index (BMI) is:
Using pseudocode or otherwise, write an algorithm that will input the ID, weight (kg) and
height (m) of 30 students, calculate their body mass index (BMI) and output their ID, BMI and
a comment as follows:
A BMI greater than 25 will get the comment OVER WEIGHT, a BMI between 25 and 19
(inclusive) will get NORMAL and a BMI less than 19 will get UNDER WEIGHT.
Question 02:
Fuel economy for a car is found using the formula:
Write an algorithm, using pseudocode or otherwise, which inputs the Distance Travelled (km)
and the Fuel Used (litres) for 1000 cars. The Fuel Economy for each car is then calculated and
displayed.
Question 03:
The manufacturing cost of producing an item depends on its complexity. A company
manufactures three different types of item, with costs based on the following calculations:
Item type 1: item cost = parts cost * 1.5
Item type 2: item cost = parts cost * 2.5
Item type 3: item cost = parts cost * 5.0
The company makes 1000 items per day.
Write an algorithm, using pseudocode, flowchart or otherwise, which
inputs the item type and parts cost of each item
outputs the item cost for each item
Counting
In many question you will be asked to find some sub-set of data such as input 5 numbers and
find how many were positive and negative. Counting usually depends on some condition
where we increment the respective counter by 1 if condition gets true. We initialize each
counter (i.e. CountA, CountB) for respective category (i.e. CountA to record occurrence of
positive numbers and CountB to record occurrence of negative numbers) by 0 at first. Never
mix counting with Count Variable. Count Variable just controls the iteration; it got nothing to
do with category counting. Counting variables i.e. CountA, CountB etc will always be printed
once loop gets over. It will never be printed within loop.
Write an algorithm in the form of
a flowchart which takes
temperatures input over a 100
day period (once per day) and
outputs the number of days when
the temperature was below 20C
and the number of days when the
temperature was 20C and above.
Start
Count0
CountA0
CountB0
Yes
is
Count=100
No
Input
Temp
Yes
is
Temp>=20
No
CountBCountB+1
CountACountA+1
CountCount+1
Output
CountA, CountB
End
Counting:
This algorithm inputs 5 values and outputs how many input numbers were negative and how
many were positive
Start
Count0
CountA0
CountB0
Yes
is
Count=5
No
Input
Num
Yes
is
Num>0
No
CountACountA+1
CountBCountB+1
CountCount+1
Output
CountA, CountB,
End
Practice Questions:
Question 01:
A company has 5000 CDs, DVDs, videos and books in stock. Each item has a unique 5-digit
code with the first digit identifying the type of item, i.e.
1 = CD
2 = DVD
3 = video
4 = book
For example, for the code 15642 the 1 identifies that it is a CD, and for the code 30055 the 3
identifies that it is a video.
Write
an algorithm, using pseudocode or otherwise, that
Inputs the codes for all 5000 items
Validates the input code
Calculates how many CDs, DVDs, videos and books are in stock
Outputs the four totals.
Question 02:
5000 numbers are being input which should have either 1 digit (e.g. 5), 2 digits (e.g. 36), 3
digits (e.g. 149) or 4 digits (e.g. 8567).
Write an algorithm, using pseudocode or flowchart only, which
inputs 5000 numbers
outputs how many numbers had 1 digit, 2 digits, 3 digits and 4 digits
outputs the % of numbers input which were outside the range
Question 03:
A company is carrying out a survey by observing traffic at a road junction. Each time a car,
bus, lorry or other vehicle passed by the road junction it was noted down.
10 000 vehicles were counted during the survey.
Write an algorithm, using pseudocode, which:
inputs all 10000 responses
outputs the number of cars, buses and lorries that passed by the junction during the
survey
outputs the number of vehicles that werent cars, buses or lorries during the survey
Totaling
Totaling is usually done when you have to calculate average. In totaling we add the value
entered by user or derived from any calculation. Calculation of average and result of total
and average will be placed outside the loop. Never calculate average or show result of total
or average inside the loop.
Write an algorithm in the form of a flowchart which takes
temperatures input over a 100 day period (once per day)
and outputs the number of days when the temperature was
below 20C and the number of days when the temperature
was 20C and above, also print average temperature of 100
days.
Start
Count0
Temp0
CountA0
CountB0
Total0
Avg0
is
Yes
Count=100
No
Input
Temp
Total=Total+Temp
Yes
is
Temp>=20
No
CountBCountB+1
CountACountA+1
CountCount+1
Avg(Total/100)
Output
CountA, CountB,
Avg
End
Practice Questions:
Question 01:
A school is doing a check on the heights and weights of all its students. The school has 1000
students.
Write an algorithm, using pseudocode or a flowchart, which
inputs the height and weight of all 1000 students
outputs the average (mean) height and weight
includes any necessary error traps for the input of height and weight
Question 02:
Write an algorithm, using pseudocode, which inputs rainfall (in cm) for 500 days and outputs
the average rainfall.
Question 03:
Fuel economy for a car is found using the formula:
Write an algorithm, using pseudocode or otherwise, which inputs the Distance Travelled (km)
and the Fuel Used (litres) for 1000 cars. The Fuel Economy for each car is then calculated and
the following outputs produced:
Fuel Economy for each car
average (mean) Fuel Economy for all of the cars input
Extreme Values
On many occasion you will be asked to find highest or lowest values or both. We call it
finding extreme values i.e. maximum/minimum, highest/lowest, fastest/slowest etc. For
example Write an algorithm in the form of a flowchart which takes temperatures input over
a 100 day period (once per day) and outputs the highest and lowest temperature recorded
in these 100days.
Note: We always initialize lowest value with 1000 instead of 0 and values of Highest/Lowest
will never be released within the loop.
Start
Count0
High0
Low1000
Yes
is
Count=100
No
Input
Temp
Yes
is
No
Temp>High
HighTemp
Yes
is
Temp<Low
LowTemp
CountCount+1
Output
High,
Low
End
No
Practice Questions:
Question 01:
A cars speed is measured between points A and B, which are 200 km apart.
The final speed of the car is calculated using the formula:
What is the final speed of a car if it takes 2 hours to get from A to B?
[1]
(b) Write an algorithm, using pseudocode or otherwise, which inputs the times for 500 cars,
calculates the final speed of each car using the formula in part (a), and then outputs:
the final speed for ALL 500 cars
the slowest (lowest) final speed
the fastest (highest) final speed
Question 02:
Write an algorithm, using pseudocode or flowchart only, which:
inputs three numbers
outputs the largest of the three numbers
Question 03:
As part of an experiment, a school measured the heights (in metres) of all its 500 students.
Write an algorithm, using pseudocode, which inputs the heights of all 500 students and
outputs the height of the tallest person and the shortest person in the school.