An algorithm is a step-by-step procedure or formula for solving a problem.
It involves various
constructs and statements to guide the process of computation. Here’s a breakdown of the key
components involved in an algorithm:
1. Attributes and Constructs of an Algorithm
Attributes:
o Input: The data that the algorithm takes in to process.
o Output: The result produced by the algorithm.
o Definiteness: Each step of the algorithm is precisely defined.
o Finiteness: The algorithm terminates after a finite number of steps.
o Effectiveness: Each step must be basic enough to be carried out with a pencil and
paper.
Constructs:
o Sequential: Instructions are executed one after another.
o Selection: Making decisions (if-then-else statements).
o Iteration: Looping (for, while loops).
o Invocation: Invoking other algorithms or procedures.
2. Statements in an Algorithm
a. Input
An algorithm starts by receiving inputs, which are necessary data to process.
Input: Read the values of a, b, and c.
b. Output
An algorithm produces outputs as a result of its computations.
Output: Print the result.
c. Decision Making (Selection)
Decision making involves branching logic based on conditions.
If (a > b)
Print "a is greater than b"
Else
Print "a is not greater than b"
d. Looping (Iteration)
Looping involves repeating a set of instructions until a condition is met.
For i from 1 to 10
Print i
Algorithm: Sum of first n natural numbers
plaintext
Copy code
1. Start
2. Input: Read the value of n
3. Initialize sum = 0
4. For i from 1 to n
sum = sum + i
5. Output: Print the value of sum
6. Stop