Algorithm and Flowchart
Algorithm :
An algorithm is a set of step by step instructions to perform particular task or to generate solution to a particular problem.
Characteristics :
1) Input : An algorithm can accept zero or more inputs. It is not compulsory to provide input to the algorithm. For example if we are writing algorithm for displaying general welcome message to user then there is no need to accept any input. Thus algorithm can have zero inputs also.
2) Output : An algorithm must produce at least one output. If your algorithm does not produces any output then it is of no use.
3) Definiteness : Each and every instruction mentioned in an algorithm should be clearly specified. Instructions in an algorithm should not generate any ambiguity (confusion) for user. Thus definiteness is an important property of an algorithm.
4) Finiteness : Number of instructions written in an algorithm should be finite or you can say limited. If number of instructions in a set of algorithm is very huge (large in count) then algorithm becomes complex and difficult to understand for user. Thus finiteness is also an important property that algorithm possess.
5) Simplicity : An algorithm should be simple so that it can be carried out by using only paper and pencil.
Some examples of Algorithm :
1) Write an algorithm to display "Good Morning" message.
Solution :
Step 1: Start
Step 2: display Good Morning using printf statement.
Step 3: Stop
(Note : This is also example of algorithm with zero input)
2) Write an algorithm to compute addition of two integers.
Solution :
Step 1: Start
Step 2: Accept two integers as a input from user (say a,b).
Step 3: Calculate addition of accepted input (for example c = a + b)
Step 4: Display result to the user.
Step 5: Stop
(Note : Write an algorithm to compute subtraction, multiplication and division of two integers as an assignment.)
3) Write an algorithm to calculate addition first 'n' natural numbers.
Solution :
Step 1: Start
Step 2: Accept value of 'n' from user.
Step 3: Initialize variable say i to 1 (i=1 since natural numbers starts from 1) and sum=0 (to store addition of n natural numbers)
Step 4: Check whether i<=n if yes then go to Step 5 else go to Step 7
Step 5: calculate sum = sum + i.
Step 6: Go to Step 4 again
Step 7: display value of sum to user.
Step 8: Stop
(Note : Above algorithm is using loop to calculate sum.)
4) Write an algorithm to check whether a particular number is even or odd.
Solution :
Step 1: Start
Step 2: Accept number for user say n
Step 3: Check whether n%2 equals zero or not that is whether given number is completely divisible by 2 or not. If yes then go to Step 4 else go to Step 5.
Step 4: Display "Number is Even" message to user.
Step 5: Display "Number is Odd" message to user.
Step 6: Stop
(Note : Here we are making use of condition checking statement if.)
5) Write an algorithm to perform menu driven operations (+, - , / , *) on two integers.
Solution :
Step 1: Start
Step 2: Accept two integers (say n1,n2) and which operation to perform (say ch) on them using Menu " Choose operation to perform 1. Addition 2. Subtraction 3. Multiplication, 4. Division 5. Exit Enter your choice " from the user.
Step 3: Compare value of ch if it is 1 then calculate display addition of given numbers to user, if it is 2 then calculate display subtraction of given numbers to user, if it is 3 then calculate display multiplication of given numbers to user, if it is 4 then calculate display division of given numbers to user, if it is 5 then go to Step 4.
Step 4: Stop
(Note: Here we are using multiway decision making statement switch)
Nice mam👍
ReplyDelete