Monday, 20 January 2014

Sequence and Decision in Algorithm.

Sequence means that each step or process in the algorithm is executed in the specified order.In the Kettle example,each process must be in the proper place otherwise the algorithm will fail.

The decision constructs -if...then,if...then...else...

In algorithms the outcome of a decision is either true or false;there is no state in between.The outcome of the decision is based on some condition that can only result in a true or false value.
For example:
if today is Monday then collect pay
Here,the above statement is a decision and the decision takes the general form: if proposition then process
A proposition,in this sense,is a statement,which can only be true or false.It is either true that 'today is Monday' or it is false.If the proposition is true,then the process or procedure that follows the proposition is executed.
The decision can also be stated as:
if proposition
                   then process 1
else             process 2
This  is the if...then...else... form of decision.This means that if the proposition is true,then execute process 1,else execute process 2.
The first form of the decision if proposition then process has a null else,that is,there is no else.

Let us take an example to understand sequence and decision.

Q1)Write an algorithm to check whether a number given by user is even or odd.
Solution: Let the number to be checked be represented by 'N'.
The number 'N' is divided by 2 to give an integer quotient,denoted by Q.If the remainder,designated as 'R' is zero,'N' is even,otherwise 'N' is odd.This logic is applied in following algorithm.
  1. START
  2. [Ask Input]
    Print "Enter the number"
  3. [Take Input]
    Input N
  4. [Logic Applied to find whether number is even or odd]
    Q <- N/2 (i.e. Q=N/2)
  5. [Finding remainder]
    R <- N-Q*2 (i.e. R=N-Q*2)
  6. [Decision Construct]
    If R = 0 then Print "N is EVEN"
  7. [Decision Construct] If R!=0 then Print "N is ODD"
  8. STOP

Q2)Print the largest among three numbers.
Solution: Let the three number be represented  by A,B, and C.There can be three ways of solving the problem.The three algorithms with some differences are given below:
   Type 1: By using AND operator.
  1. START
  2. Print "Enter the Number"
  3. Input A,B,C
  4. If A >= B AND B>=C
    Then print A
  5. If B >= C AND C >= A
    Then Print B
    ELSE Print C
  6. STOP
   Type 2: This algorithm uses a variable MAX to store the largest number.
  1. START
  2. Print "Enter THREE NUMBERS"
  3. Input A,B,c
  4. MAX <-A (i.e. MAX=A)
  5. If B > MAX Then MAX <- B (i.e. MAX=B)
  6. If C > MAX Then MAX <- C (i.e. MAX=C)
  7. Print MAX
  8. STOP
Type 3: This algorithm uses a nested if construct.
  1. START
  2. Print "Enter three number"
  3. Input A,B,C
  4. If A > B then
    If A > C Then
    Print A
    Else Print C
    Else If B > C Then
    Print B
    Else Print C
  5. STOP
If you face any problem regarding ALGORITHM,inform us and we will try to solve it out.

NOTE:More examples of ALGORITHM will be updated only on user demand.


Please don't forget to share this information with your friends.



Subscribe to our Newsletter

Contact our Support

Email us:

Our Team Memebers