Robotics & Artificial Intelligence
Write a user-defined function def Perfect() to input a number from the user. The program checks whether it is a perfect number or not. Finally, it returns the values to main program to display the result.
A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself.
For example, 6 = 1 + 2 + 3
It is a perfect number.
Python Functions
1 Like
Answer
def Perfect(n):
s = 0
for i in range(1, n):
if(n % i == 0):
s = s + i
return(s)
# main program
num = int(input("Enter a number:"))
sum1 = Perfect(num)
if(sum1 == num):
print("It is a Perfect number")
else:
print("It is not a perfect number")Output
Enter a number:6
It is a Perfect number
Answered By
3 Likes
Related Questions
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The following program is designed to generate next five terms of the Fibonacci series, excluding 0 and 1 (i.e., first two terms of the series). The incomplete user-defined program is given as under:
def Fibo_Series(): a = 0 b = 1 count = ........... # Line 1 while(count <= ............): # Line 2 c = a + b print(.............) # Line 3 a = ............. # Line 4 b = c count = count + 1 # main program Fibo_Series()Now, complete the above program to generate next five numbers of the series as 1, 2, 3, 5, 8.
Write a user-defined function
def Circle()to input radius of a circle from the user. The program calculates area and circumference inside the function. Finally, it returns the values to main program to display the result.Write a user-defined function
def Sum_Digits()to input a number from the user. The program calculates sum of the digits of the number inside the function without returning any data to the main program.Sample Input: 345
Output: 3 + 4 + 5 = 12
Write a user-defined function
def Rev_Digits()to input a number from the user. The program reverses the digits to produce a new number inside the function. However, it displays the new number inside the function without returning any data to the main program.Sample Input: 257
Output: 752