determine whether a number is a perfect number an armstrong number or a palindrome in python
Answers
added a txt file as brainly found text it innapropriate. open and paste the text in python
Answer:
The below python code checks whether a number is a perfect number an Armstrong number or a palindrome:
def perfect_number(n):
summ=0
for i in range(1,n):
if(n%i==0):
summ+=i
if(summ==n):
return("Number is perfect number")
else:
return("Number is not a perfect number")
def armstrong(n):
num = str(n)
length = len(num)
for i in range(1,length):
s += num[i]**length
if(s==n):
return("Number is Armstrong number")
else:
return("Number is not Armstrong number")
def palindrome(n):
num1 = str(n)
if(num1==num1[::-1]):
return ("Number is a palindrome")
else:
return("Number is not palindrome")
#DRIVER CODE
number = int(input())
print(perfect_number(number))
print(armstrong(number))
print(palindrome(number))
Explanation:
PERFECT NUMBER
A number is said to be a perfect number if the sum of its positive divisors (excluding the number) is equal to the number.
For example:
6 is the perfect number because its divisors sum 1+2+3 =6
ARMSTRONG NUMBER
A number is an Armstrong number if the sum of its digits with the power of length or order of the number is equal to the number.
For example:
153 is an Armstrong number. Because the order of the number is 3
and 1x1x1 + 5x5x5 + 3x3x3 = 153
PALINDROME NUMBER
A number or string is a palindrome if the reverse of the number is equal to the original number.
For example:
121 is a palindrome number. Because its reverse is also 121.
In the above code, I just mapped these properties with the respective functions and checked the number that comes in which categroty.
#SPJ3