Introduction of Algorithm

What is an Algorithm?

The word "Algorithm" relates to the name of the mathematician Al-Khowarizmi, which implies a procedure or a technique. In another words algorithm is a step by step writing a program in your own words. An algorithm may be a sequence of steps to resolve a specific problem or algorithm is an ordered set of unambiguous steps that produces a result and terminates during a finite time.

Algorithm has the following characteristics:



1) Input: An algorithm may or may not require input.

2) Output: Each algorithm is expected to produce at least one result

3) Definiteness: Each instruction must be clear and unambiguous.

4) Finiteness: If the instructions of an algorithm are executed, the algorithm should terminate after finite number of steps

5) Effectiveness: Every Step must be basic and essential.

Advantages of an Algorithm

1) It could be a step-wise representation of an answer to a given problem, which makes it easy to understand.

2) An algorithm uses a definite procedure.

3) It is not dependent on any programming language, so it's easy to know for anyone even without having prior programming knowledge.

4) Every step in an algorithm has its own logical sequence so it's easy to debug.

How to write Algorithms:

Step 1 :

Clearly define your algorithms input:  Many algorithms requires data for processing, e.g. Algorithm to calculate the area of rectangle here input will be the height and width.

Step 2 :

Clearly define the variables used : Algorithm's variables allow you to use it for more than one place. Here we have to define two variables for rectangle height and width named as HEIGHT and WIDTH. Before declaring a variable name we should keep in mind that the variable name should be meaningful. e.g. instead of using H & W use HEIGHT and WIDTH as variable name.

Step 3 :

Outline the algorithm's operations: We should use input variable for computation purpose, e.g. for finding the area of rectangle we have to multiply the HEIGHT and WIDTH variable and then store the resultant value in new variable (say) AREA. An algorithm's operations can take the form of multiple steps and even branch, depending on the value of the input variables.

Step 4 :

Output the results of your algorithm's operations: In the above case of area of rectangle output will be the multiplication of HEIGHT  and WIDTH stored in variable AREA. if the input variables described a rectangle with a HEIGHT=2 and a WIDTH=3, the algorithm would output the value of AREA=6.

There are three types of control structures.

Sequence: In sequence structure, statements are placed one after the other and the execution takes place using top to down approach.

Selection(Branching): In selection control, if there is a condition and according to a condition, decision either TRUE or FALSE is achieved. In the case of TRUE, one of the two branches is executed; but in the case of FALSE condition, the other alternative is executed. Basically , the ‘IF-THEN’ is used to represent branch control.

Repetition(Loop): The Loop or Repetition allows a statement(s) to be executed again and again based on certain condition.

Examples:

# Algorithm for finding the sum of two numbers

first number : number1
second number : number2

Step 1: Start
Step 2: Input first numbers say number1.
Step 3: Input second number say numbers2.
Step 4: Sum = number1 + number2.
Step 5: Display Sum.
Step 6: Stop 

# Algorithm to convert temperature from Celsius to  Fahrenheit

C: Temperature in Celsius
F:  Temperature in Fahrenheit
   
Step 1: Start
Step 2: Input temperature in Celsius say C.
Step 3: F=(9.0/5.0 * C) + 32
Step 4: Display Temperature in Fahrenheit F.
Step 5: Stop

# Algorithm to find the area and perimeter of square.

L: Side, Length of square
Area: Area of square
Perimeter: Perimeter of square

Step 1: Start
Step 2: Input side length of square say L
Step 3: Area =L*L
Step 4: Perimeter = 4* L
Step 5: Display Area, Perimeter
Step 6: Stop