Page 76 - 2502_Pakistan-kifayat_C-7
P. 76

Example 2: Flowchart to add first 100 natural numbers.


                                                                   Start




                                                                  Sum = 0



                                                                    N = 0



                                                                 N = N + 1




                                                              Sum = Sum + N




                                                                     Is
                                                                  N > 100?
                                                       No


                                                                         Yes

                                                                 Print Sum




                                                                    Stop




                    SOLVING PROBLEMS USING ALGORITHMS AND FLOWCHARTS


              Example 1: Write an algorithm and draw a flowchart to calculate the factorial of a number.

              Algorithm

              Step 1:   Start
              Step 2:   Read the number n

              Step 3:   Initialize i=1, fact=1
              Step 4:   Check If i < = n Then Repeat Step 5 and 6 until i=n

              Step 5:   fact=fact*i
              Step 6:   i=i+1

              Step 7:   Print fact
              Step 8:   Stop







                  74  Premium Edition-VII
   71   72   73   74   75   76   77   78   79   80   81