Chapter – 7 : Algorithm
Algorithm
An algorithm is a finite, ordered set of clear instructions used to solve a problem or perform
a task.
It is a step-by-step process that gives a solution in a finite amount of time.
Examples of Algorithm
1. Algorithm to add two numbers
2. Algorithm to find largest of three numbers
3. Algorithm to calculate average marks
4. Algorithm to check even or odd number
Characteristics of a Good Algorithm
1. Input
An algorithm must have one or more inputs.
○ Example: Input two numbers a and b
○ Example: Input marks of a student
2. Output
It must produce at least one output.
○ Example: Display sum
○ Example: Display result (Pass/Fail)
3. Definite
Each step must be clear.
○ Example: “Add a and b” (clear)
○ Wrong: “Do calculation” (not clear)
4. Termination
The algorithm must stop after finite steps.
○ Example: Stop after printing output
○ Example: End after result is displayed
5. Effectiveness
Steps must be simple and executable.
○ Example: Multiply numbers
○ Example: Compare two values
Algorithm to Make Tea
1. Take water in kettle
2. Boil the water
3. Add tea leaves
4. Add sugar
5. Add milk
6. Mix properly
7. Let it boil and wait
8. Serve the tea
More Daily-Life Algorithm Examples
1. Algorithm to brush teeth
2. Algorithm to switch on a computer
Problem Solving Steps
1. Understand the problem
2. Identify the inputs and outputs
3. Design an algorithm
4. Represent it using a flowchart
5. Test the solution
6. Improve it if needed
Example
Problem: Find sum of two numbers
● Input: a, b
● Output: sum
Simple Algorithm Example (Addition)
1. Start
2. Input a and b
3. Sum = a + b
4. Display sum
5. Stop
Flowchart
A flowchart is a graphical representation of an algorithm using standard symbols.
Advantages of Flowchart
1. Easy to understand
2. Helps to detect errors
3. Useful for communication
4. Good for beginners
5. Shows logic clearly
6. Makes debugging easier
Flowchart Symbols
● Oval (Terminal) – Start / Stop
● Parallelogram – Input / Output
● Rectangle (Process) – Calculation / Instruction
● Diamond (Decision) – Yes / No condition
● Flow Line (Arrow) – Direction of flow
Pseudocode
Pseudocode is structured English used to describe an algorithm without following
programming syntax.
Why We Use Pseudocode
1. Easy to write
2. Easy to understand
3. Language independent
4. Close to real programming logic
5. Helps in converting to actual code
Pseudocode Keywords & Examples
● input – take input
○ input a
○ input marks
● output – display output
○ output sum
○ output result
● if then else – decision making
● while – condition controlled loop
● for – count controlled loop
● endif – end of if
● endwhile – end of while
● endfor – end of for
Example Pseudocode (Even/Odd)
input n
if n mod(%) 2 = 0 then
output "Even"
else
output "Odd"
endif
Stepwise Refinement
It is a top-down approach where a complex problem is broken into smaller and simpler
steps.
Advantages
1. Reduces complexity
2. Improves clarity
3. Makes debugging easier
4. Encourages structured thinking
Example: Calculate Average
Simple Steps
1. Input 3 numbers
2. Add the numbers
3. Divide sum by 3
4. Display average
Detailed Algorithm
1. Start
2. Input a, b, c
3. sum = a + b + c
4. average = sum / 3
5. Display average
6. Stop
_______
Programming Concepts
Variables
A variable is a named memory location used to store data that can change during program
execution.
Why Variables Are Needed
1. Store user input
2. Store intermediate results
3. Store final output
Rules for Naming Variables
1. Must start with a letter
2. Can contain underscore (_)
3. No spaces allowed
4. Should not be a keyword
5. Meaningful names should be used
Examples
● name = "Vedic"
● age = 15
● marks = 90
● total_sum = 200
Constants
A constant is a value that does not change during program execution.
Why Constants Are Used
1. Avoid accidental changes
2. Improve readability
3. Make program easy to maintain
Examples
● PI = 3.14
● DAYS = 7
● MAX = 100
● SCHOOL_NAME = "ABC School"
Data Types
A data type specifies what type of data a variable can store.
Types & Examples
1. Integer – whole numbers
○ 10, -5, 0
2. Float – decimal numbers
○ 38.5, 4.75
3. String – text or characters
○ "Name", "India"
4. Boolean – True / False
○ True, False
Operators
Operators are symbols used to perform operations on data.
Types of Operators
1. Arithmetic Operators
○ (Addition)
○ (Subtraction)
○ (Multiplication)
● / (Division)
Examples:
● a+b
● x*y
2. Relational Operators
Used for comparison.
● == (Equal to)
● < (Less than)
● >(Greater than)
● <= (Less than equal to)
● >= (Greater than equal to)
● != (not equal to)
Examples:
● a == b
● marks >= 40
3. Logical Operators
● AND
● OR
● NOT
Examples:
● a > 5 AND b < 10
● NOT True
Selection (Decision Making)
Selection means choosing an action based on a condition.
Types
1. IF
2. IF ELSE
3. Nested IF
4. IF ELSE IF
Example
If marks >= 40 → Pass
Else → Fail
Loops
A loop repeats a block of code until a condition is satisfied.
For Loop
Used when the number of repetitions is known.
Examples:
● Print numbers 1 to 10
● Print table of 5
While Loop
Used when number of repetitions is not known.
Condition is checked before execution.
Examples:
● Read numbers until user enters 0
● Keep asking password until correct
Arrays
An array is a data structure that stores multiple values of the same data type under one
name.
Where Arrays Are Used
1. Store many values efficiently
2. Easy access using index number
Arrays are stored using [ ] (square brackets).
Examples
● marks[5]
● numbers[10]
● age[20]
● score[50]