Page 64 - 2502_Pakistan-kifayat_C-8
P. 64
Step 5: Print "The number is odd".
Step 6: Stop
Example 5: Check if a Person is Eligible to Vote Based on Age.
Step 1: Start
Step 2: Input the age (Age).
Step 3: Check If Age > =18, then go to step 4, otherwise go to step 5.
Step 4: Print "You are eligible to vote".
Step 5: Print "You are not eligible to vote".
Step 6: Stop
CREATING ALGORITHMS FOR COMPLEX PROBLEMS
Let us consider examples to create algorithms for complex problems.
Example 6: Calculate the Sum of numbers from 1 to 10.
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 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
Example 7: Convert Binary to Decimal
Step 1: Start
Step 2: Input the binary number (e.g., as a string or array of digits).
Step 3: Set decimal = 0.
Step 4: Set position = 0 (this will track the power of 2 starting from the rightmost digit).
Step 5: Repeat the following steps for each digit in the binary number from right to left:
a. Convert the current binary digit to integer (0 or 1).
b. Multiply the digit by 2 position .
c. Add the result to decimal.
d. Increment position by 1.
Step 6: After all digits are processed, print or return decimal.
Step 7: Stop
62 Premium Edition-VIII

