What is an Algorithm with Examples

In this article, we will discuss what is an Algorithm with examples.

What is an Algorithm?

Algorithm is a method of representing the step-by-step logical procedure for solving a problem. A program written in a non-computer language is called an algorithm. It is a step-by-step method of performing any task.

These are one of the most basic tools that are used to develop the program-solving logic. They can have steps that repeat or require decisions until the task is completed.

PROPERTIES

An algorithm must possess the following properties:
1) Fitness: An algorithm must terminate in a finite number of steps.
2) Definite: By definite we mean that each step of the algorithm must be precisely defined such that there is no ambiguity or contradiction.
3) Effectiveness: Each step must be effective, easily converted into a program statement, and can be performed exactly in a finite amount of time.
4) Generality: The algorithm must be complete so that it will work successfully in solving all the problems of the particular type for which it is defined.
5) Input/Output: Each algorithm must take zero, one or more quantities an input data, and produce one or more output values.

While writing an algorithm we can concentrate only on the logic and not on language syntax and once the algorithm is written we can code the algorithm in computer language. 

Example 1: Write an algorithm to find the sum and average of two numbers

Step 1. Read the numbers a, b
Step 2. Compute the sum of a & b
Step 3. Store the result in variable s 
Step 4. Divide the sum s by 2
Step 5. Store the result in variable avg 
Step 6. Print the value of s and avg 
Step 7. End of program.

Example 2: Find Area, Diameter, and Circumference of a Circle.

Step 1: Start
Step 2: Initialize PI to 0
Step 3: Read radius of the circle
Step 4: Calculate the product of radius with itself and PI value
Step 5: Store the result in variable area
Step 6: Calculate the product of radius with 2
Step 7: Store the result in variable diameter
Step 8: Calculate the product of PI and diameter value
Step 9: Store the result in variable circumference
Step 10: Print the value of area, diameter and circumference
Step 11: Stop

Example 3: Find Area and Circumference of a Rectangle

Step 1: Start
Step 2: Read the values of length and breadth
Step 3: Calculate the product of length and breadth
Step 4: Store the result in variable area
Step 5: Calculate the sum of length and breadth
Step 6: Store the result in variable temp
Step 7: Calculate the product of 2 and temp
Step 8: Store the result in variable circumference
Step 9: Print the value of area and circumference
Step 10: Stop



Comments