Algorithms 2
Algorithms 2
PROGRAMMING ALGORITHMS
The algorithm
•An algorithm is a finite sequence of instructions which can be carried out to solve a
particular problem in order to obtain the desired results.
A finite sequence of instructions
The program must be planned before writing.
Represents the logic of the program
Instructions are to be written in the proper sequence.
Expressed in the programming language, becomes the program
1. Characteristics of an algorithm
•An algorithm must satisfy the following criteria:
1)Input: Zero or more quantities are externally supplied as inputs.
2)Output: At least one quantity as output is produced.
3)Finiteness: An algorithm terminates after a finite number of steps.
4)Definiteness: Each instruction must be clear and unambiguous.
5)Effectiveness: Every instruction must be very basic so that it can b carried out in
principle, by a person using just a pencil and paper.
• Let us consider few example of creating algorithm for a given problem.
Examples
•Example 1: Algorithm for sum of two
numbers Step 1: Take the first number
from the user (input).
•Step 2: Take the second number from the
user (input). Step 3: Add the two numbers to
get the sum (process). Step 4: Display the
sum on the screen (output).
Example 2:
•Write an algorithm to check whether the entered number is
positive, negative or zero.
Step 1: Take the number from the user
Step 2: If the number is less than 0,
then display the entered number is negative.
Step 3: If the number is greater than 0,
display the entered number is positive.