Python Program for Armstrong, Prefect Number, Palindrome

Write a program to determine whether a number is a perfect number, an Armstrong number or a palindrome in Python. This python project is useful for beginners and CBSE KV School Class 11 and Class 12 students computer science practicals file and NIELIT O Level Programming and Problem Solving through Python (Module M3-R5).

Perfect number

perfect number is a positive integer that is equal to the sum of its positive divisors, 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

Armstrong number

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers

Palindrome

palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar

Objective : Write a Python program to determine whether a number is a perfect number, an Armstrong number or a palindrome

Screenshot of the program:

perfect number, an armstrong number or a palindrome in Python

Here is the explanation of program

NOTICE- The indentation in the explanation is different from the one given in source code just below it. If you are going to run the program download source code file because the indentation is correct there.

First go above and read what palindrome number , armstrong and perfect number means

def palindrome(n): # palindrome a function is made and given the input number
temp = n #temp is taken as input
rev = 0 #reverse is 0
while n > 0: #if n is greater than 0 this loop runs
dig = n % 10
rev = rev * 10 + dig #This loop calculates the reverse and matches it with input
n = n // 10
if temp == rev:
print(temp, “The number is a palindrome!”)
else:
print(temp, “The number isn’t a palindrome!”)

def armstrong(n):
count = 0
temp = n
while temp > 0:
digit = temp % 10
count += digit ** 3
temp //= 10 # Armstrong number is a number that is equal to the sum of cubes of its digits
if n == count:
print(n, “is an Armstrong number”)
else:
print(n, “is not an Armstrong number”)

def perfect(n):
count = 0
for i in range(1, n):
if n % i == 0:
count = count + i
if count == n:
print(n, “The number is a Perfect number!”)
else:
print(n, “The number is not a Perfect number!”)

if name == ‘main‘:
n = int(input(“Enter number:”))
palindrome(n) #a perfect number is a positive integer that is equal to the sum of its positive divisors
armstrong(n)
perfect(n)

for Source code whether a number is a perfect number, an armstrong number or a palindrome in Python – Click here

Output of the program

perfect number, an armstrong number or a palindrome in Python

For more Program for beginners and KV CBSE School Class 11 and Class 12 students computer science practical file and NIELIT O Level Programming and Problem Solving with Python (Module M3-R5).click here

Hello World Program in Python

Input a welcome message and display it in Python

Display the larger / smaller number in Python.

Greatest of Three Numbers in Python using Nested if

Patterns using nested loop in Python

Program to Print Pattern in Python

Program to input the value of x and n and sum of series in Python

Python Program for Armstrong, Prefect Number, Palindrome

Program of Prime number by recursion in python

Prime Number Program in Python

Write a Program to Print Fibonacci Series in Python

LCM and GCD program in Python

Python program to count number of vowels in a string

Whether a String is Palindrome or not in Python

Bubble Sort Program in Python

Linear search in python using list

Program to read a text file in python

Python program to read a file line by line

Program to Count Vowels and Consonants in Python

Stack Program in Python

Queue Program in Python

Python Leap Year Program for Beginners

Python Program to Print Series and Addition

Binary Search Program in Python

Program to find sum of digits in python

Sum of numbers divisible by 3 and 5 in python