Page 505 - ComputerScience_Class_11
P. 505

Program 4: Write a program to enter any number and check if it is a Krishnamurthy number or not. If any number is
                 equal to sum of factorial of its digits, then it is a Krishnamurthy number.
                 Example: 145=1!+4!+5!
                     Program 4.py

                  File  Edit  Format   Run   Options   Window     Help

                  n=int(input('Enter a number: '))
                  s=0
                  c=n
                  while c>0:
                      f=1
                      d=c%10
                      for i in range(1,d+1):
                          f=f*i
                      s=s+f
                      c=c//10
                  if s==n:
                      print(n,'is a Krishnamurthy number')
                  else:
                      print(n,'is not a Krishnamurthy number')



                     Output

                  Enter a number: 40585
                  40585 is a Krishnamurthy number



                 Program 5: Write a program to print the following pattern based on the user’s choice:
                 Pattern 1: 1                        Pattern 2: 1 2 3 4
                             1 2                               2 3 4
                             1 2 3                             3 4
                             1 2 3 4                           4
                 If the user enters 1, print the pattern 1, if the user enters 2, print pattern 2 else print invalid number.

                     Program 5.py
                  File  Edit  Format   Run   Options   Window     Help

                  a = int(input('Enter your choice: '))
                  if a == 1:
                      #pattern 1
                      for i in range(1, 5):
                          for j in range(1, i+1):
                              print(j,end=' ')
                          print()
                  elif a == 2:
                      #pattern 2
                      for i in range(1, 5):
                          for j in range(i, 5):
                              print(j,end=' ')
                          print()
                  else:
                      print('Invalid number')






                                                                                                    Flow of Control  503
   500   501   502   503   504   505   506   507   508   509   510