Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 131 - 2609_Logicode
P. 131

    d.   A teacher asks Devesh to calculate the sum of all numbers between 1 and 100. Which loop would
                            he use? Explain why one loop is better suited for this task than the other.

                        e.   Siya wants to repeat a block of statements for a given number of times until the control condition
                            is false. For this, what type of loop can she use?

                                                                                         21 st
                                                                                       Century   #Critical Thinking
                                                                                        Skills
                       Write the output of the following programs:
                       1.  x=1
                            sum=0
                            while(x<=10):
                                sum=sum+x
                                 x=x+1
                            print (sum)
                       2.  i = 2
                            while True:
                                if i%3 == 0:
                                    break
                                print(i)
                                i += 2
                       3.  i = 0
                            while i < 5:
                                print(i)
                                i += 1
                                if i == 3:
                                    break
                                else:
                                     print(0)

                                                                                                  21 st   #Critical Thinking
                        LAB ACTIVITY                                                            Century   #Interdisciplinary Learning
                                                                                                 Skills

                         Write a program to:

                         ▶   find the largest number out of three greatest number.
                         ▶  print the numbers 8, 11, 14, 17, 20, . . . .
                         ▶  print 1 to 100 using while loop.

                         ▶  find out the sum of numbers within the range 1 to 100 using while loop.
                         ▶  calculate the sum of first ten even numbers by using while loop and for loop.
                         ▶  print first 20 odd numbers in decreasing order using while loop.

                                                                                           Looping Statements in Python  129

| 126 127 128 129 130 131 132 133 134 135 136 |