Page 89 - 2502_Pakistan-kifayat_C-6
P. 89
Example 7: Calculate the sum of numbers from 1 to 10.
Algorithm:
Step 1: Start.
Step 2: Set the variable sum = 0
Step 3: Set the counter variable 'i' = 1
Step 4: If counter variable 'i' <= 10, go to Step 5; otherwise, go to Step 7.
Step 5: Add the value of 'i' to sum (i.e., sum = sum + i).
Step 6: Increment the counter variable 'i' by 1 and go to Step 4.
Step 7: Print the value of sum.
Step 8: Stop.
This algorithm uses a looping mechanism (via steps 4 to 6) to add all the numbers from 1 to 10. It
starts with 'i' = 1, and each time, it adds 'i' to sum and increments 'i' by 1 until 'i' exceeds 10. The sum is
calculated step-by-step, and the final sum is printed at the end.
Example 8: Print Even Numbers from 1 to 10.
Algorithm:
Step 1: Start.
Step 2: Set the counter variable 'i' = 2.
Step 3: If counter variable 'i' <= 10, go to Step 4; otherwise, go to Step 6.
Step 4: Print the value of 'i'.
Step 5: Increment the counter variable 'i' by 2 and go to Step 3.
Step 6: Stop.
This algorithm uses a looping mechanism to print all even numbers from 1 to 10. By starting with i =
2 and incrementing by 2, the algorithm prints 2, 4, 6, 8, and 10 in sequence.
Example 9: Print Multiplication Table of a Number.
Algorithm:
Step 1: Start.
Step 2: Set the counter variable 'i' = 1.
Step 3: Input the number (Num).
Step 4: If counter variable 'i' <= 10, go to Step 5; otherwise, go to Step 7.
Step 5: Print Num × i.
Step 6: Increment 'i' by 1 and go to Step 4.
Step 7: Stop.
This algorithm prints the multiplication table for the number Num. It repeats the multiplication for 'i'
values from 1 to 10 and prints the results. After each print, 'i' is incremented, and the loop continues
until all 10 multiplications have been printed.
#Algorithmic Thinking 87

