Page 121 - 2620_Birla Open Mind C-6
P. 121

SOLVING PROBLEMS USING ALGORITHMS AND FLOWCHARTS


                 Some examples are given below to solve the problems using algorithms and flowcharts:
                 Example 1: Write an algorithm and draw a flowchart to check if a given number is even or odd.


                                Algorithm:                               Flowchart:
                 Step 1:  Start.
                                                                             Start
                 Step 2:  Read Number.

                 Step 3:  Check If Number % 2 == 0 Then
                                                                       Read a Number
                          print "Number is even."

                          Else
                                                                              Is
                          print "Number is odd."                                          Yes
                                                                       Number%2==0?             Print Number is even
                 Step 4:  End.

                                                                                No


                                                                     Print Number is odd



                                                                             Stop



                 Example 2: Write an algorithm and draw a flowchart to find the smallest of two numbers A and B.


                                  Algorithm:                               Flowchart:

                 Step 1:  Start.                                              Start

                 Step 2:   Read two numbers and store them
                          in A and B.
                                                                         Read A and B
                 Step 3:   Compare number  A  and  B.  If  A  is

                          less than B, then

                           print "A is smallest"                               if          Yes    Print A is smallest
                                                                              A < B
                          Else
                          print "B is smallest"                                  No

                 Step 4:  End.
                                                                       Print B is smallest



                                                                              Stop








                                                                                             #Algorithmic Intelligence 119
   116   117   118   119   120   121   122   123   124   125   126