Page 93 - TP_Pluse_V2.2_Class_6
P. 93

SOLVING PROBLEMS USING ALGORITHMS AND FLOWCHARTS



                 Example 1: Write an algorithm and draw a            Example  2: Write an  algorithm  and  draw  a
                 flowchart to calculate the factorial of a number.   flowchart to check whether a given number is
                                                                     even or odd.
                 Algorithm
                                                                     Algorithm
                 Step 1:  Start
                                                                     Step 1:  Start
                 Step 2:  Read the number n
                 Step 3:  Initialize i=1, fact=1                     Step 2:  Read Number
                 Step 4:   Check If i < = n Then Repeat Step 5       Step 3:  Check If Number % 2 == 0 Then
                         to 6 until i=n                                      print "Number is Even."

                 Step 5:  fact=fact*i                                        Else
                 Step 6:  i=i+1                                              print "Number is Odd."
                 Step 7:  Print fact                                 Step 4:  Stop
                 Step 8:  Stop                                       Flowchart:
                 Flowchart:

                              Start                                                        Start




                                                                                     Read a Number
                             Read n




                               i = 1                                                        Is

                             fact = 1                                                 Number%2==0



                                                                                    Yes            No
                                Is
                                            False
                             i < = n?                                    Print Number              Print Number
                                                                             is even                   is odd


                                  True

                           fact = fact*i
                                                  Print fact                               Stop




                              i = i + 1





                              Stop



                                                                                           Algorithm and Flowchart  91
   88   89   90   91   92   93   94   95   96   97   98