Page 121 - 2622_Delhi Police Public School_C-8
P. 121

SOME MORE PROGRAMS

                 Program 10: To print a pattern using while loop.











                     Program10.py

                  File  Edit  Format   Run   Options   Window    Help

                  #Program to show a pattern using while loop.
                  a = 5
                  while a>= 1:
                      print("@"*a)
                      a -= 1





                 On executing the above program, you will get the following output:

                     Output

                  @@@@@
                  @@@@
                  @@@
                  @@
                  @





                 Program 11: To add first five natural numbers.


                     Program11.py
                  File  Edit  Format   Run   Options   Window    Help

                  sum=0
                  a=1
                  while (a<=5):
                      sum=sum+a

                      a=a+1
                  print("the sum of the first five natural numbers is", sum)











                                                                                                 Loops in Python   119
   116   117   118   119   120   121   122   123   124   125   126