From JOYCE, 2 Months ago, written in Python.
Embed
Hits: 152
1. '''Q.1) Write an interactive python program to perform the following
2. (a) to input an integer and check whether it is a perfect no or not. checkperfect()
3. (b) to pass an integer argument and check whether it’s a prime no or not. checkprime(n)
4. (c) to pass an integer argument and check whether it is an Armstrong or not. checkarmstrong(n)
5. (d) Exit from the menu and quit the program.'''
6. def checkperfect():
7.      s=0
8.      for i in range(1,n):
9.           if n%i==0:
10.                s+=i
11.      if n==s:
12.           print('Perfect number')
13.      else:
14.           print('Not Perfect number')
15. def checkprime(n):
16.      for i in range(2,n):
17.           if n%i==0:
18.                print('Not a prime number')
19.                break
20.      else:
21.           print('Prime number')
22. def checkarmstrong(n):
23.      m=n
24.      s=0
25.      while n!=0:
26.           d=n
27.           s=s+d**3
28.           n=n//10
29.      if m==s:
30.           print('Armstrong number')
31.      else:
32.           print('Not a armstrong number')
33. #--main--#
34. ch=1
35. while ch!=4:
36.     print("1.To check perfect number or not")
37.     print("2.To check prime number or no")
38.     print("3.To check armostrong or not")
39.     print("4.To Exit")
40.     ch=int(input("Enter choice:"))
41.     if ch==1:
42.         n=int(input("Enter a number"))
43.         checkperfect()
44.     elif ch==2:
45.         n=int(input("Enter a number"))
46.         checkprime(n)
47.     elif ch==3:
48.         n=int(input("Enter a number"))
49.         checkarmstrong(n)
50.     elif ch!=4:
51.         print("Invalid choice")
52.     print("-"*20)
53.
54. ''' OUTPUT'''
55. 1.To check perfect number or not
56. 2.To check prime number or no
57. 3.To check armostrong or not
58. 4.To Exit
59. Enter choice: 1
60. Enter a number 4
61. Not Perfect number
62. --------------------
63. 1.To check perfect number or not
64. 2.To check prime number or no
65. 3.To check armostrong or not
66. 4.To Exit
67. Enter choice: 1
68. Enter a number 6
69. Perfect number
70. --------------------
71. 1.To check perfect number or not
72. 2.To check prime number or no
73. 3.To check armostrong or not
74. 4.To Exit
75. Enter choice: 2
76. Enter a number 2
77. Prime number
78. --------------------
79. 1.To check perfect number or not
80. 2.To check prime number or no
81. 3.To check armostrong or not
82. 4.To Exit
83. Enter choice: 3
84. Enter a number 636
85. Not a armstrong number
86. --------------------
87. 1.To check perfect number or not
88. 2.To check prime number or no
89. 3.To check armostrong or not
90. 4.To Exit
91. Enter choice: 3
92. Enter a number 371
93. Armstrong number
94. -------------------
95. 1.To check perfect number or not
96. 2.To check prime number or no
97. 3.To check armostrong or not
98. 4.To Exit
99. Enter choice:4
100. --------------------