Page 440 - Ai_417_V3.0_C9_Flipbook
P. 440

S. No.                                          Program
                    19    Input a number and check if the number is positive, negative or zero and display an appropriate
                          message

                          N=int(input("Enter a number: "))
                          if N<0:
                            print(N," is a -ve number")
                          elif N==0:
                                                                                       Output:
                            print(N," is entered")
                                                                                       Enter a number: 5
                          else:
                                                                                       5  is a +ve number
                            print(N," is a +ve number")
                    20    To print first 10 natural numbers

                          for i in range(1,11):
                             print(i,end="!")
                                                                                       Output:
                                                                                       1!2!3!4!5!6!7!8!9!10!

                    21    To print first 10 even numbers                               Output:
                                                                                       2
                          for i in range(1,11):
                                                                                       4
                             print(2*i)
                                                                                       6
                                                                                       8
                                                                                       10
                                                                                       12
                                                                                       14
                                                                                       16
                                                                                       18
                                                                                       20

                    22    To print N odd numbers
                          N=int(input("Enter the term : "))
                                                                                       Output:
                          for i in range(1,N+1):                                       Enter the term : 7
                              print(2*i-1,end=",")                                     1,3,5,7,9,11,13,
                    23    To print sum of first 10 natural numbers

                          S=0
                          for i in range(1,11):
                                                                                       Output:
                             S+=i
                                                                                       The sum is  55
                          print("The sum is ",S)
                    24    Program to find the sum of all numbers stored in a list

                          S=0
                          for i in [1,2,3,4,5]:
                                                                                       Output:
                              S+=i
                                                                                       The sum is  15
                          print("The sum is ",S)




                    438     Touchpad Artificial Intelligence (Ver. 3.0)-IX
   435   436   437   438   439   440   441   442   443   444   445