Page 121 - TP_Prime_v2.2_Class_6
P. 121

Solving Problems using Algorithms and Flowcharts


                 Example 1: Write an algorithm and draw              Example 2: Write an algorithm and draw a
                 a flowchart to calculate the factorial of           flowchart to check whether a given number
                 a number.                                           is even or odd.

                 Algorithm                                           Algorithm

                 Step 1:  Start                                      Step 1:  Start
                 Step 2: Read the number n                           Step 2: Read the number n                             PROGRAMMING LANGUAGES  PROBLEM SOLVING AND
                 Step 3: Initialise i=1, fact=1                      Step 3: Check if n % 2 == 0 then

                 Step 4:  Check If i < = n then repeat Step 5                 print "Number is Even."
                          & 6 until i=n                                       Else

                 Step 5: fact=fact*i                                          print "Number is Odd."
                 Step 6: i=i+1                                       Step 4: Stop

                 Step 7: Print fact                                  Flowchart:                                           119
                 Step 8: Stop

                 Flowchart:                                                             Start


                               Start
                                                                                        Read n


                              Read n

                                                                                          Is
                               i = 1                                                   n % 2==0

                              fact = 1

                                                                                Yes                 No

                                 Is           No
                              i < = n?                                 Print "Number             Print "Number
                                                                          is Even."                 is Odd."


                                   Yes
                           fact = fact*i           Print fact
                                                                                         Stop


                              i = i + 1




                               Stop
   116   117   118   119   120   121   122   123   124   125   126