Page 236 - Computer Science Class 11 Without Functions
P. 236

Case Based Questions

           1.  Pushpreet has a deep interest in Mathematics and is also inclined towards computing skills. He wants to write a program in
              Python that computes and displays the sum of the following series for an arbitrary n (he will provide the value of n when
              the program is executed):
               sum = 1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4) +  (1 + 2 + 3 + 4 + …+n)
               Write a program for Pushpreet to complete the given task.
         Ans:
              '''
              Objective: To compute the sum of first n terms of the series
              Input: num - numeric value
              Output: sum of series - numeric value
              '''
              num = int(input('Enter last number in the series: '))

              total = 0
              for i in range(2,  num+2):
                  term = 0
                  for j in range(1,   i):
                      term += j
                  total = total + term

              print('Sum of ',   num,   'terms is:' ,   total)
           2.  Sambhav works in the marketing department of an IT firm named Diamond Corporation. For the starting slide of his
              presentation,   his manager wants to display the following pattern on the screen. The manager wants the size of the pattern
              to be generic so that it can be decided later.
                                                         *
                                                        ***
                                                       *****
                                                      *******
                                                       *****
                                                        ***
                                                         *
              Sambhav has been assigned a task to write a program in Python to display the given pattern. Help him complete the task.
         Ans: '''
              Objective: To display a diamond pattern
              Input: n - numeric value
              Output: diamond pattern
              '''

              n = int(input('Enter the number: '))
              for i in range(n):
                  for j in range(n-i-1):
                      print(' ',  end='')
                  for j in range(2*i + 1):
                      print('*',  end='')
                  print()

              for i in range(n-1):
                  for j in range(i+1):

         234   Touchpad Computer Science-XI
   231   232   233   234   235   236   237   238   239   240   241