Write a python program to determine whether number is perfect number ,an Armstrong number or a Palindrome number using for loop
What is perfect number?
According to Wikipedia, In number theory, a perfect number is a positive integer that is equal to the sum of its positive divisor, excluding the number itself. For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.
In other definition, a perfect number is a number that is half the sum of all of its positive divisors including itself; For instance 28 is perfect number as 1+2+4+7+14+28 = 56 . Here, 28 has divisors 1,2,4,7,14,28(including itself), and 56 = 2 * 28 . that’s why 28 is perfect number.
Examples of perfect numbers are 6,28,496,8128 and many more.
Python program to determine whether number is perfect number or not
This Python program for Perfect Number allows the user to enter any number. Using this number it will calculate whether the number is Perfect number or not using the Python if-else Loop.
Program Logic:
- Take a any number from user using input method
- Use for loop to iterate through number
- Use if statement within for loop to add the proper divisors of the integer to the sum variable.
- Compare the original value with Sum value.
- If they exactly matched, then it is an perfect number else it is not perfect.
Program code:
n = int(input("Enter any number to check whether it is perfect number or not : "))
sum = 0
# Check for perfect number
for i in range(1,n):
if n%i==0:
sum = sum + i
if sum == n :
print( n,"is perfect number")
else :
print( n, "is not perfect number")
What is an Armstrong number?
According to Wikipedia, in a number theory, If the given number is equal to the sum of the cube of each digit present in that integer, then that number can be an Armstrong Number in Python.
For example, 153 is Armstrong Number; 153 number is 3 digit number
so sum of cube of each digit = original number
= 13 +53 +33
= 1 + 125 + 27
= 153 Hence; 153 = 153
For Example, 152 is not Armstrong number; 152 is 3 digit number
= 13 +53 +23
= 1 + 125 + 8
= 134 ; Hence 134 is not equal to 152
so sum of cube of each digit is not equal to original number. that’s why 152 is not Armstrong number
Python program to check whether given number is an Armstrong number or not
Problem Statement:
This Python program allows the user to enter any positive integer. And then, checks whether a number is Armstrong Number or Not using the While Loop
Program Logic:
- Take any number from user using input method.
- Count the Number of individual digits (For Example, 153 means 3 digit).
- Divide the given number into individual digits (For Example, Divide 153 into 1, 5, and 3 ).
- Calculate the power of n for each individual and add those numbers.
- Compare the original value with Sum value.
- If they exactly matched, then it is an Armstrong number else it is not Armstrong.
#check for armstrong number
n = int(input("Enter any number to check whether it is an armstrong : "))
temp = n
total = 0
while temp > 0 :
digit = temp %10
total = total + (digit**3)
temp = temp//10
if n == total:
print( n,"is an armstrong number")
else :
print( n, "is not armstrong number")
What is Palindrome number?
In number theory,If the given number is equal to reverse of digit of number,then that number can be Palindrome number in python. In other word, Palindrome number is number that remain same when its digit are reversed.
For Example, 141 is Palindrome number; 141 is 3 digit number.
reverse of digit of number = original number
When we reverse its digit ,we get same number that’s why 141 is palindrome number.
For Example,142 is not Palindrome number; When we reverse its digit, we get 241 which is not equal to 142 that’s why 142 is not palindrome number.
Example of Palindrome numbers : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, …
Python program to check whether given number is palindrome number or not
This Python program allows the user to enter any positive integer. And then, checks whether a number is Palindrome Number or Not using the While Loop
Program Logic:
- Take any number from user using input method
- Store number in temporary variable say temp.
- Count the Number of individual digits (For Example, 121 means 3 digit).
- Divide the given number into individual digits (For Example, Divide 121 into 1, 2, and 1).
- Calculate the reverse of number by adding remainder
- Compare the original number with reverse number
- If they exactly matched, then it is Palindrome number else it is not Palindrome.
Program Code:
#check for palindrome number
n = int(input("Enter any number to check whether it is palindrome : "))
temp = n
rev = 0
while n > 0:
d = n % 10
rev = rev *10 + d
n = n//10
if temp == rev :
print( temp,"is palindrome number")
else :
print( temp, "is not palindrome number")
Lets combine above 3 source code to determine given number is perfect,an armstrong or palindrome number.
Write a python program to determine given number is perfect,an armstrong or palindrome number using if-else,for and while loop
n = int(input("Enter any number to check whether it is perfect ,armstrong or palindrome : "))
sum = 0
# Check for perfect number
for i in range(1,n):
if n%i==0:
sum = sum + i
if sum == n :
print( n,"is perfect number")
else :
print( n, "is not perfect number")
#check for armstrong number
temp = n
total = 0
while temp > 0 :
digit = temp %10
total = total + (digit**3)
temp = temp//10
if n == total:
print( n,"is an armstrong number")
else :
print( n, "is not armstrong number")
#check for palindrome number
temp = n
rev = 0
while n > 0:
d = n % 10
rev = rev *10 + d
n = n//10
if temp == rev :
print( temp,"is palindrome number")
else :
print( temp, "is not palindrome number")
Below is snapshot of output
