Total Pageviews

Wednesday, September 9, 2026

algorithms,

```html

🐍 ALGORITHM

Definition • Characteristics • Steps • Examples • Advantages

1. What is an Algorithm?

An Algorithm is a finite sequence of well-defined and unambiguous steps used to solve a particular problem or perform a specific task.

An algorithm describes what should be done step by step before the actual Python program is written.

Simple Idea:
Problem → Algorithm → Flowchart → Python Program → Output

2. Characteristics of an Algorithm

1️⃣

Input

An algorithm may accept zero or more inputs.

2️⃣

Output

It should produce at least one meaningful result.

3️⃣

Definiteness

Every step must be clear and unambiguous.

4️⃣

Finiteness

The algorithm must terminate after a finite number of steps.

5️⃣

Effectiveness

Each step must be simple enough to be carried out.

6️⃣

Generality

It should solve a class of similar problems rather than only one particular input.

3. Steps for Writing an Algorithm

  1. Understand the Problem
    Clearly identify what the problem is asking.
  2. Identify Inputs
    Determine what data is required.
  3. Identify Output
    Determine what result must be produced.
  4. Develop the Logic
    Decide the operations and decisions required.
  5. Write the Steps
    Arrange the operations in the correct sequence.
  6. Check the Algorithm
    Test the steps using sample data.
  7. Convert into Program
    Implement the algorithm using Python or another programming language.

4. Example 1 – Addition of Two Numbers

Problem

Write an algorithm to add two numbers.

Algorithm

  1. Start.
  2. Input the first number A.
  3. Input the second number B.
  4. Calculate SUM = A + B.
  5. Display SUM.
  6. Stop.

Python Program

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

sum = a + b

print("Sum =", sum)

Output

Enter first number: 10 Enter second number: 20 Sum = 30

5. Example 2 – Find the Largest of Two Numbers

Algorithm

  1. Start.
  2. Input A and B.
  3. Compare A and B.
  4. If A is greater than B, display A.
  5. Otherwise, display B.
  6. Stop.

Python Program

a = int(input("Enter A: "))
b = int(input("Enter B: "))

if a > b:
    print("Largest =", a)
else:
    print("Largest =", b)

Output

Enter A: 45 Enter B: 30 Largest = 45

6. Example 3 – Check Even or Odd

Algorithm

  1. Start.
  2. Input a number N.
  3. Calculate N % 2.
  4. If the remainder is 0, display Even.
  5. Otherwise, display Odd.
  6. Stop.

Python Program

n = int(input("Enter a number: "))

if n % 2 == 0:
    print("Even Number")
else:
    print("Odd Number")

Output

Enter a number: 18 Even Number

7. Example 4 – Calculate Factorial

Algorithm

  1. Start.
  2. Input N.
  3. Set FACT = 1.
  4. Repeat from 1 to N.
  5. Multiply FACT by the current number.
  6. Display FACT.
  7. Stop.

Python Program

n = int(input("Enter a number: "))

fact = 1

for i in range(1, n + 1):
    fact = fact * i

print("Factorial =", fact)

Output

Enter a number: 5 Factorial = 120

8. Algorithm vs Program

Algorithm Program
Step-by-step solution to a problem. Actual implementation of the solution.
Usually written in simple language. Written using a programming language.
Language independent. Language dependent.
Focuses on logic. Focuses on executable instructions.
Can be converted into different programs. Runs according to a particular language environment.

9. Algorithm vs Flowchart

Algorithm Flowchart
Written step-by-step procedure. Graphical representation of the procedure.
Uses statements or natural language. Uses standard symbols.
Easy to modify. Modification may require redrawing.
Good for detailed steps. Good for visual understanding.
Does not require graphical symbols. Requires standard graphical symbols and arrows.

10. Advantages of Algorithm

✅ Advantages

  • Easy to understand.
  • Provides a clear solution strategy.
  • Helps in program planning.
  • Language independent.
  • Easy to test using sample data.
  • Helps identify logical errors.
  • Useful before writing actual code.
  • Acts as documentation.
  • Makes complex problems easier to break into steps.
  • Can be converted into a flowchart or program.

❌ Disadvantages

  • Complex problems may require lengthy algorithms.
  • Writing detailed algorithms can be time-consuming.
  • It does not directly execute on a computer.
  • Different programmers may describe the same logic differently.
  • Very detailed algorithms may become difficult to read.
  • Frequent changes may require rewriting several steps.

11. Rules for Writing a Good Algorithm

  1. Start with a clear first step.
  2. Use simple and understandable language.
  3. Each step should have a clear meaning.
  4. Avoid ambiguous statements.
  5. Maintain the correct sequence of operations.
  6. Clearly identify input and output.
  7. Ensure that the algorithm terminates.
  8. Test the algorithm with sample values.
  9. Use meaningful variable names.
  10. Keep unnecessary steps out of the algorithm.

12. Important Points for Examination

  • An algorithm is a finite sequence of well-defined steps used to solve a problem.
  • The five important characteristics are input, output, definiteness, finiteness and effectiveness.
  • Algorithms are generally language independent.
  • An algorithm describes the logic before coding.
  • Algorithms can be represented using flowcharts.
  • A good algorithm should be clear, finite and effective.
  • Every step should be unambiguous.

13. Quick Summary

Algorithm = A finite sequence of clear and well-defined steps used to solve a problem.

Characteristics:
Input → Output → Definiteness → Finiteness → Effectiveness

Program Development:
Problem → Algorithm → Flowchart → Python Code → Output

A well-designed algorithm makes programming easier, reduces logical errors and provides a clear roadmap for implementation.
```

No comments:

Post a Comment