Page 444 - AI Ver 3.0 class 10_Flipbook
P. 444

[2]:   i = 1
                      while i < 6:
                          print(i)
                          i += 1
                      1
                      2
                      3
                      4
                      5


               [3]:   #input a number and check for even or odd.
                      #The whole process should continue till the user wants.
                      ans = 'y'
                      while ans == 'y':
                          Num = int(input("Enter a number: "))
                          if Num%2 == 0:
                              print (Num," is an even number")
                          else:
                              print (Num," is an odd number")
                          ans = input("Enter y to continue: ")
                      print ("Program ends here")
                      Enter a number:  20
                      20  is an even number
                      Enter y to continue:  y
                      Enter a number:  21
                      21  is an odd number
                      Enter y to continue:  n
                      Program ends here


               [4]:   #program to reverse the digits of a number
                      Num = int(input("Enter a number: "))
                      Dig = 0
                      while Num>0:
                          R = Num%10
                          Dig = (Dig*10)+R
                          Num //= 10
                      print(Dig)

                      Enter a number:  5291
                      1925


               [5]:   # Program to add natural
                      # numbers up to
                      # sum = 1+2+3+ ... +n
                      n = int(input("Enter the value of n: "))
                      sum = 0
                      i = 1
                      while i <= n:
                          sum = sum + i
                          i += 1
                      print("The sum is", sum)

                      Enter the value of n:  20
                      The sum is 210




                    442     Touchpad Artificial Intelligence (Ver. 3.0)-X
   439   440   441   442   443   444   445   446   447   448   449