Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🧩 Unit 2 – Problem Solving  ·  Lesson 09

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.

🍵 Daily-life algorithm: making tea

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

1. Analyse the problemWhat is given (input)? What is needed (output)?← understand2. Develop an algorithmwrite the step-by-step solution in plain language← plan3. Codeconvert the algorithm into Python← build4. Test and debugrun with many inputs, fix errors← check
Four steps of 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

📥
InputTakes zero or more clearly defined inputs.
📤
OutputProduces at least one result.
🎯
DefinitenessEach step is clear and has only one meaning.
⏹️
FinitenessStops after a finite number of steps.
✅
EffectivenessEvery step is simple enough to be done exactly.
🔁
CorrectnessGives the right answer for all valid inputs.
A good algorithm has all these

Algorithm examples

Example 1: Area of a rectangle

  1. Start
  2. Input length L and breadth B
  3. Area ← L × B
  4. Print Area
  5. Stop

Example 2: Check whether a number is even or odd

  1. Start
  2. Input N
  3. If N mod 2 = 0, print "Even", otherwise print "Odd"
  4. Stop

Example 3: Largest of three numbers

  1. Start
  2. Input A, B, C
  3. Largest ← A
  4. If B > Largest then Largest ← B
  5. If C > Largest then Largest ← C
  6. Print Largest
  7. Stop

Example 4: Factorial of N (N! = 1 × 2 × … × N)

  1. Start
  2. Input N
  3. Fact ← 1, i ← 1
  4. Repeat while i ≤ N: Fact ← Fact × i; i ← i + 1
  5. Print Fact
  6. 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.

Stepii ≤ N ?Fact
start1—1
loop 11True1 × 1 = 1
loop 22True1 × 2 = 2
loop 33True2 × 3 = 6
loop 44True6 × 4 = 24
check5False → exit24
Trace table: 4! = 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
Both give the same answer, but B is far faster
Python
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')
500000500000 500000500000 Loop steps: 1000000 | 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.

Python
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)
First number: 25 Second number: 71 Third number: 48 Largest = 71

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.
✅ Practice

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.

Frequently Asked Questions

What is an algorithm in simple words?
A finite list of clear, step-by-step instructions that takes some input and produces the required output, like a recipe for solving a problem.
What is a dry run?
Executing an algorithm by hand with sample input, writing the value of every variable after each step in a trace table, to check that it gives the correct output.
Why do we compare algorithms?
Many algorithms can solve the same problem. We compare how much time (number of steps) and memory each needs, and pick the most efficient one.
← Class 11 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicFlowchart, Pseudocode & Decomposition Your progress: 9 / 29 (31%) · Lesson 10 / 29 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free