Algorithm Practice Exercises
Practice your algorithmic thinking with these real-world problems. For each exercise, try to write:
- The algorithm (step-by-step instructions)
- The pseudocode (structured format)
- Optionally, draw the flowchart
Exercise 1: Daily Morning Routine
Problem: Write an algorithm for your morning routine from waking up to leaving for school/college.
Hint: Think about the decisions you make (Is it a school day? What's the weather like?)
Your Solution:
BEGIN MorningRoutine
Step 1: _________________
Step 2: _________________
Step 3: _________________
...
END
👁️ Sample Solution
Algorithm:
- Wake up
- Check if it's a school day
- If yes, brush teeth and get ready
- If no, go back to sleep or relax
- Check weather
- If raining, take umbrella
- If sunny, wear sunglasses
- Have breakfast
- Pack bag
- Leave for school
Pseudocode:
BEGIN MorningRoutine
WAKE up
IF today is school day THEN
brush teeth
get dressed
ELSE
relax
END IF
CHECK weather
IF raining THEN
take umbrella
ELSE IF sunny THEN
wear sunglasses
END IF
HAVE breakfast
PACK bag
LEAVE for school
END
Exercise 2: Calculating Simple Interest
Problem: Write an algorithm to calculate simple interest using the formula: SI = (P × R × T) / 100
Inputs: Principal amount (P), Rate of interest (R), Time period (T) Output: Simple Interest
Your Solution:
BEGIN CalculateSimpleInterest
INPUT _________________
interest = _________________
PRINT _________________
END
👁️ Sample Solution
Algorithm:
- Accept Principal amount (P)
- Accept Rate of interest (R)
- Accept Time period (T)
- Calculate interest = (P × R × T) / 100
- Display the calculated interest
Pseudocode:
BEGIN CalculateSimpleInterest
INPUT principal, rate, time
interest = (principal * rate * time) / 100
PRINT interest
END
Flowchart:
Exercise 3: Checking Even or Odd Number
Problem: Write an algorithm to determine if a given number is even or odd.
Hint: Use the modulo operator (%) - if number % 2 = 0, it's even
Your Solution:
BEGIN CheckEvenOdd
INPUT number
IF _________________ THEN
PRINT "Even"
ELSE
_________________
END IF
END
👁️ Sample Solution
Algorithm:
- Accept a number from user
- Divide the number by 2
- If remainder is 0, number is even
- Otherwise, number is odd
- Display the result
Pseudocode:
BEGIN CheckEvenOdd
INPUT number
IF number MOD 2 = 0 THEN
PRINT "Even"
ELSE
PRINT "Odd"
END IF
END
Exercise 4: Finding the Smallest Number
Problem: Write an algorithm to find the smallest of three numbers.
Your Solution:
BEGIN FindSmallest
INPUT num1, num2, num3
smallest = _________________
IF _________________ THEN
smallest = _________________
END IF
IF _________________ THEN
smallest = _________________
END IF
PRINT smallest
END
👁️ Sample Solution
Algorithm:
- Accept three numbers
- Assume first number is smallest
- Compare with second number - if second is smaller, update smallest
- Compare with third number - if third is smaller, update smallest
- Display the smallest number
Pseudocode:
BEGIN FindSmallest
INPUT num1, num2, num3
smallest = num1
IF num2 < smallest THEN
smallest = num2
END IF
IF num3 < smallest THEN
smallest = num3
END IF
PRINT smallest
END
Exercise 5: ATM Machine Withdrawal
Problem: Write an algorithm for an ATM machine to process cash withdrawal.
Requirements:
- Check if PIN is correct
- Check if sufficient balance exists
- Check if daily withdrawal limit is not exceeded
- Dispense cash if all conditions are met
Your Solution:
BEGIN ATMWithdrawal
INPUT PIN
IF _________________ THEN
INPUT amount
IF _________________ THEN
IF _________________ THEN
DISPENSE cash
UPDATE balance
PRINT "Success"
ELSE
PRINT "Daily limit exceeded"
END IF
ELSE
PRINT "Insufficient balance"
END IF
ELSE
PRINT "Invalid PIN"
END IF
END
👁️ Sample Solution
Algorithm:
- Accept PIN from user
- Verify PIN with bank records
- If PIN is correct, proceed
- Accept withdrawal amount
- Check if account has sufficient balance
- Check if amount is within daily limit
- If both conditions are met, dispense cash
- Update account balance
- Display success message
- If any condition fails, display appropriate error message
Pseudocode:
BEGIN ATMWithdrawal
INPUT PIN
IF PIN is valid THEN
INPUT amount
IF amount <= balance THEN
IF amount <= daily_limit THEN
DISPENSE cash
balance = balance - amount
PRINT "Withdrawal successful"
ELSE
PRINT "Daily limit exceeded"
END IF
ELSE
PRINT "Insufficient balance"
END IF
ELSE
PRINT "Invalid PIN"
END IF
END
Exercise 6: Grading System
Problem: Write an algorithm to assign grades based on marks:
| Marks | Grade |
|---|---|
| 90-100 | A |
| 80-89 | B |
| 70-79 | C |
| 60-69 | D |
| Below 60 | F |
Your Solution:
BEGIN AssignGrade
INPUT marks
IF marks >= 90 AND marks <= 100 THEN
grade = "A"
ELSE IF _________________ THEN
grade = "B"
ELSE IF _________________ THEN
grade = "C"
ELSE IF _________________ THEN
grade = "D"
ELSE
_________________
END IF
PRINT grade
END
👁️ Sample Solution
Algorithm:
- Accept marks from user
- If marks are between 90-100, assign grade A
- If marks are between 80-89, assign grade B
- If marks are between 70-79, assign grade C
- If marks are between 60-69, assign grade D
- If marks are below 60, assign grade F
- Display the assigned grade
Pseudocode:
BEGIN AssignGrade
INPUT marks
IF marks >= 90 AND marks <= 100 THEN
grade = "A"
ELSE IF marks >= 80 AND marks <= 89 THEN
grade = "B"
ELSE IF marks >= 70 AND marks <= 79 THEN
grade = "C"
ELSE IF marks >= 60 AND marks <= 69 THEN
grade = "D"
ELSE
grade = "F"
END IF
PRINT grade
END
Exercise 7: Temperature Converter
Problem: Write an algorithm to convert temperature from Celsius to Fahrenheit.
Formula: F = (C × 9/5) + 32
Your Solution:
BEGIN CelsiusToFahrenheit
INPUT celsius
fahrenheit = _________________
PRINT _________________
END
👁️ Sample Solution
Algorithm:
- Accept temperature in Celsius
- Apply formula: Fahrenheit = (Celsius × 9/5) + 32
- Display the result in Fahrenheit
Pseudocode:
BEGIN CelsiusToFahrenheit
INPUT celsius
fahrenheit = (celsius * 9/5) + 32
PRINT fahrenheit
END
Exercise 8: Password Validator
Problem: Write an algorithm to validate a password with these rules:
- At least 8 characters long
- Contains at least one digit
- Contains at least one special character
Your Solution:
BEGIN ValidatePassword
INPUT password
IF length >= 8 THEN
IF _________________ THEN
IF _________________ THEN
PRINT "Valid password"
ELSE
PRINT "Missing special character"
END IF
ELSE
PRINT "Missing digit"
END IF
ELSE
_________________
END IF
END
👁️ Sample Solution
Algorithm:
- Accept password from user
- Check if password length is at least 8 characters
- Check if password contains at least one digit
- Check if password contains at least one special character
- If all conditions are met, password is valid
- Otherwise, display specific error message
Pseudocode:
BEGIN ValidatePassword
INPUT password
IF length(password) >= 8 THEN
IF contains_digit(password) THEN
IF contains_special_char(password) THEN
PRINT "Valid password"
ELSE
PRINT "Password must contain a special character"
END IF
ELSE
PRINT "Password must contain a digit"
END IF
ELSE
PRINT "Password must be at least 8 characters"
END IF
END
Exercise 9: Shopping Cart Total
Problem: Write an algorithm to calculate the total cost of items in a shopping cart.
Requirements:
- Add prices of all items
- Apply discount if total exceeds ₹1000 (10% discount)
- Add delivery charge if total is below ₹500 (₹50 delivery charge)
- Display final amount
Your Solution:
BEGIN ShoppingTotal
total = sum of all item prices
IF _________________ THEN
total = _________________
END IF
IF _________________ THEN
total = _________________
END IF
PRINT total
END
👁️ Sample Solution
Algorithm:
- Calculate sum of all item prices
- If total exceeds ₹1000, apply 10% discount
- If total is below ₹500, add ₹50 delivery charge
- Display the final amount
Pseudocode:
BEGIN ShoppingTotal
total = sum of all item prices
IF total > 1000 THEN
total = total - (total * 0.10)
END IF
IF total < 500 THEN
total = total + 50
END IF
PRINT total
END
Exercise 10: Factorial Calculation
Problem: Write an algorithm to calculate the factorial of a number.
Definition: n! = n × (n-1) × (n-2) × ... × 1 Example: 5! = 5 × 4 × 3 × 2 × 1 = 120
Your Solution:
BEGIN CalculateFactorial
INPUT number
factorial = _________________
FOR i FROM 1 TO number DO
_________________
END FOR
PRINT factorial
END
👁️ Sample Solution
Algorithm:
- Accept a number from user
- Initialize factorial = 1
- Multiply factorial by each number from 1 to the given number
- Display the result
Pseudocode:
BEGIN CalculateFactorial
INPUT number
factorial = 1
FOR i FROM 1 TO number DO
factorial = factorial * i
END FOR
PRINT factorial
END
Challenge Exercises
Challenge 1: Binary Search Algorithm
Write an algorithm to find a specific number in a sorted list using binary search.
Hint: Binary search repeatedly divides the search interval in half.
Challenge 2: Sorting Algorithm
Write an algorithm to sort a list of numbers in ascending order.
Hint: Compare adjacent elements and swap if they're in wrong order.
Tips for Solving These Exercises
- Start with the algorithm in plain English first
- Break down complex problems into smaller steps
- Think about edge cases - what happens with invalid input?
- Use consistent naming for variables and steps
- Test your logic by walking through examples manually
- Draw flowcharts for complex decision-making processes
Ready for Real Code?
Once you're comfortable with these exercises, you're ready to:
- Start learning Variables & Data Types
- Understand Conditionals for decision-making
- Master Loops for repetition
- Build Functions for reusable code
The more algorithms you write, the better you'll get at thinking like a programmer. Try solving these exercises in different ways to find the most efficient approach!