🐍 ALGORITHM
Definition • Characteristics • Steps • Examples • Advantages
1. What is an Algorithm?
An algorithm describes what should be done step by step before the actual Python program is written.
Problem → Algorithm → Flowchart → Python Program → Output
2. Characteristics of an Algorithm
Input
An algorithm may accept zero or more inputs.
Output
It should produce at least one meaningful result.
Definiteness
Every step must be clear and unambiguous.
Finiteness
The algorithm must terminate after a finite number of steps.
Effectiveness
Each step must be simple enough to be carried out.
Generality
It should solve a class of similar problems rather than only one particular input.
3. Steps for Writing an Algorithm
-
Understand the Problem
Clearly identify what the problem is asking. -
Identify Inputs
Determine what data is required. -
Identify Output
Determine what result must be produced. -
Develop the Logic
Decide the operations and decisions required. -
Write the Steps
Arrange the operations in the correct sequence. -
Check the Algorithm
Test the steps using sample data. -
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
- Start.
- Input the first number A.
- Input the second number B.
- Calculate SUM = A + B.
- Display SUM.
- Stop.
Python Program
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum =", sum)
Output
5. Example 2 – Find the Largest of Two Numbers
Algorithm
- Start.
- Input A and B.
- Compare A and B.
- If A is greater than B, display A.
- Otherwise, display B.
- Stop.
Python Program
a = int(input("Enter A: "))
b = int(input("Enter B: "))
if a > b:
print("Largest =", a)
else:
print("Largest =", b)
Output
6. Example 3 – Check Even or Odd
Algorithm
- Start.
- Input a number N.
- Calculate N % 2.
- If the remainder is 0, display Even.
- Otherwise, display Odd.
- Stop.
Python Program
n = int(input("Enter a number: "))
if n % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Output
7. Example 4 – Calculate Factorial
Algorithm
- Start.
- Input N.
- Set FACT = 1.
- Repeat from 1 to N.
- Multiply FACT by the current number.
- Display FACT.
- 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
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
- Start with a clear first step.
- Use simple and understandable language.
- Each step should have a clear meaning.
- Avoid ambiguous statements.
- Maintain the correct sequence of operations.
- Clearly identify input and output.
- Ensure that the algorithm terminates.
- Test the algorithm with sample values.
- Use meaningful variable names.
- 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
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