Problem Solving & Algorithms
What is problem solving?
A computer cannot think on its own. To make it solve a problem — calculate a bill, find the topper, book a ticket — we must first understand the problem and work out the exact steps. Then we write those steps in a programming language. This whole process of identifying a problem, designing a solution and turning it into a working program is called problem solving.
1. Boil 1 cup water. 2. Add tea leaves and sugar. 3. Add milk and boil 2 minutes. 4. Strain into a cup. If you change the order (strain before boiling), you get the wrong result — order matters in algorithms too.
Steps for problem solving
Example: "Find the percentage of a student from marks of 5 subjects."
- Input: 5 marks (each out of 100).
- Process: total = sum of marks; percentage = total ÷ 500 × 100.
- Output: total and percentage.
What is an algorithm?
An algorithm is a finite sequence of well-defined steps to solve a problem. The word comes from the Persian mathematician al-Khwarizmi. An algorithm is independent of any programming language — the same algorithm can be written in Python, C++ or Java.
Characteristics of a good algorithm
Algorithm examples
Example 1: Area of a rectangle
- Start
- Input length L and breadth B
- Area ← L × B
- Print Area
- Stop
Example 2: Check whether a number is even or odd
- Start
- Input N
- If N mod 2 = 0, print "Even", otherwise print "Odd"
- Stop
Example 3: Largest of three numbers
- Start
- Input A, B, C
- Largest ← A
- If B > Largest then Largest ← B
- If C > Largest then Largest ← C
- Print Largest
- Stop
Example 4: Factorial of N (N! = 1 × 2 × … × N)
- Start
- Input N
- Fact ← 1, i ← 1
- Repeat while i ≤ N: Fact ← Fact × i; i ← i + 1
- Print Fact
- Stop
Verifying an algorithm (dry run)
Before coding, we verify the algorithm by doing a dry run: we act as the computer, take sample input, and write every variable's value in a trace table. Let us dry-run Example 4 with N = 4.
| Step | i | i ≤ N ? | Fact |
|---|---|---|---|
| start | 1 | — | 1 |
| loop 1 | 1 | True | 1 × 1 = 1 |
| loop 2 | 2 | True | 1 × 2 = 2 |
| loop 3 | 3 | True | 2 × 3 = 6 |
| loop 4 | 4 | True | 6 × 4 = 24 |
| check | 5 | False → exit | 24 |
Also test special cases: N = 0 should give 1 (the loop does not run, Fact stays 1 ✔), and N = 1 gives 1 ✔.
Comparing algorithms
Problem: find the sum 1 + 2 + … + N. Two correct algorithms:
Algorithm A – loop
- total ← 0
- for i = 1 to N: total ← total + i
- N additions
- N = 10 lakh → 10 lakh steps
Algorithm B – formula
- total ← N × (N + 1) ÷ 2
- Only 3 operations
- Same speed for any N
- Clearly more efficient
def sum_loop(n): # Algorithm A: add one by one
total = 0
for i in range(1, n + 1):
total += i
return total
def sum_formula(n): # Algorithm B: Gauss formula
return n * (n + 1) // 2
n = 1000000
print(sum_loop(n), sum_formula(n))
print('Loop steps:', n, '| Formula steps: 1')Another example: to check if N is prime, we only need to test divisors up to √N, not up to N − 1. For N = 10,007 that is about 100 checks instead of 10,000.
From algorithm to code
Here is Example 3 written in Python. Each line maps directly to one algorithm step.
a = int(input('First number: '))
b = int(input('Second number: '))
c = int(input('Third number: '))
largest = a
if b > largest:
largest = b
if c > largest:
largest = c
print('Largest =', largest)Summary & practice
- Problem solving: analyse → algorithm → code → test & debug.
- Algorithm = finite, definite, effective steps with input and output.
- Verify with a dry run and a trace table; test special cases.
- Compare algorithms by steps (time) and memory.
Write an algorithm to (1) convert temperature from Celsius to Fahrenheit (F = C × 9/5 + 32), (2) find whether a year is a leap year. Dry-run (2) with 1900, 2000 and 2024.
Hint for leap year: leap if divisible by 400, or divisible by 4 but not by 100. 1900 → not leap, 2000 → leap, 2024 → leap.
