Skip to main content

Algorithm Practice Exercises

Practice your algorithmic thinking with these real-world problems. For each exercise, try to write:

  1. The algorithm (step-by-step instructions)
  2. The pseudocode (structured format)
  3. 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:

  1. Wake up
  2. Check if it's a school day
  3. If yes, brush teeth and get ready
  4. If no, go back to sleep or relax
  5. Check weather
  6. If raining, take umbrella
  7. If sunny, wear sunglasses
  8. Have breakfast
  9. Pack bag
  10. 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:

  1. Accept Principal amount (P)
  2. Accept Rate of interest (R)
  3. Accept Time period (T)
  4. Calculate interest = (P × R × T) / 100
  5. 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:

  1. Accept a number from user
  2. Divide the number by 2
  3. If remainder is 0, number is even
  4. Otherwise, number is odd
  5. 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:

  1. Accept three numbers
  2. Assume first number is smallest
  3. Compare with second number - if second is smaller, update smallest
  4. Compare with third number - if third is smaller, update smallest
  5. 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:

  1. Accept PIN from user
  2. Verify PIN with bank records
  3. If PIN is correct, proceed
  4. Accept withdrawal amount
  5. Check if account has sufficient balance
  6. Check if amount is within daily limit
  7. If both conditions are met, dispense cash
  8. Update account balance
  9. Display success message
  10. 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:

MarksGrade
90-100A
80-89B
70-79C
60-69D
Below 60F

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:

  1. Accept marks from user
  2. If marks are between 90-100, assign grade A
  3. If marks are between 80-89, assign grade B
  4. If marks are between 70-79, assign grade C
  5. If marks are between 60-69, assign grade D
  6. If marks are below 60, assign grade F
  7. 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:

  1. Accept temperature in Celsius
  2. Apply formula: Fahrenheit = (Celsius × 9/5) + 32
  3. 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:

  1. Accept password from user
  2. Check if password length is at least 8 characters
  3. Check if password contains at least one digit
  4. Check if password contains at least one special character
  5. If all conditions are met, password is valid
  6. 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:

  1. Calculate sum of all item prices
  2. If total exceeds ₹1000, apply 10% discount
  3. If total is below ₹500, add ₹50 delivery charge
  4. 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:

  1. Accept a number from user
  2. Initialize factorial = 1
  3. Multiply factorial by each number from 1 to the given number
  4. 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

  1. Start with the algorithm in plain English first
  2. Break down complex problems into smaller steps
  3. Think about edge cases - what happens with invalid input?
  4. Use consistent naming for variables and steps
  5. Test your logic by walking through examples manually
  6. Draw flowcharts for complex decision-making processes

Ready for Real Code?

Once you're comfortable with these exercises, you're ready to:

Practice Makes Perfect

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!

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir