On this page
These number checks turn up in Class 11 practical files. The CBSE Computer Science syllabus for 2026-27 lists "Determine whether a number is a perfect number, an Armstrong number or a palindrome" among its suggested Python programs for Class XI. The same document asks for a report file of at least 20 Python programs, worth 7 of the 30 practical marks. Here is what each number is, how to check one by hand, and a short program for each.
What is an Armstrong number?
An Armstrong number is an m-digit number equal to the sum of the m-th powers of its digits (OEIS A005188). They are also called narcissistic numbers.
The positive Armstrong numbers below 10,000 are 1 to 9, 153, 370, 371, 407, 1634, 8208 and 9474 (OEIS A005188).
Base 10 has exactly 88 positive narcissistic numbers, found by D. Winter in 1985; the largest has 39 digits (MathWorld).
To check a number by hand:
Count the digits. 153 has 3, so the power is 3.
Raise each digit to that power: 1, 125 and 27.
Add them: 1 + 125 + 27 = 153. It matches, so 153 is an Armstrong number.
For 1634, with 4 digits: 1 + 1296 + 81 + 256 = 1634. Yes. For 123: 1 + 8 + 27 = 36. No.
What is a happy number?
A happy number is a positive integer whose repeated sum of squared digits reaches 1 (MathWorld). A number that is not happy is called unhappy.
Take 19. Square and add its digits: 1 + 81 = 82. Then 64 + 4 = 68. Then 36 + 64 = 100. Then 1. So 19 is happy.
Take 2 instead: 4, 16, 37, 58, 89, 145, 42, 20, and back to 4. It loops forever and never reaches 1, so 2 is unhappy. Every starting number either ends at 1 or falls into this same eight-number loop (OEIS A007770). That loop is why your program needs a way to stop.
What is a perfect number?
A perfect number equals the sum of its proper divisors, meaning every divisor except the number itself (OEIS A000396). The first four are 6, 28, 496 and 8128.
Check 28: its proper divisors are 1, 2, 4, 7 and 14, and 1 + 2 + 4 + 7 + 14 = 28. Check 12: 1 + 2 + 3 + 4 + 6 = 16, so no. Every even perfect number has the form 2^(p-1) × (2^p - 1), where 2^p - 1 is prime, and mathematicians believe no odd perfect number exists (OEIS A000396). If a prime check is next on your list, see why 1 is not a prime number.
How do you write these programs in Python?
Each program is one function; the note under each says how far to indent.
The Armstrong check:
def is_armstrong(n):
digits = str(n)
k = len(digits)
return n == sum(int(d) ** k for d in digits)
Lines 2 to 4 are indented four spaces. str(n) gives you the digits and their count in one step.
The happy check:
def is_happy(n):
seen = set()
while n != 1 and n not in seen:
seen.add(n)
n = sum(int(d) ** 2 for d in str(n))
return n == 1
Lines 2, 3 and 6 are indented four spaces. Lines 4 and 5 sit inside the while loop, so they get eight. The set remembers every number visited. When one comes round again, you are in the loop, so the loop stops and the function returns False.
The perfect check:
def is_perfect(n):
return n > 1 and n == sum(d for d in range(1, n) if n % d == 0)
Line 2 is indented four spaces. range(1, n) tries every number below n, and n % d == 0 keeps only the divisors. The n > 1 test rejects 0 and 1. Stopping at the square root and adding divisors in pairs is faster, but this version is easier to explain.
How do you test your programs before the practical exam?
Run each function on known answers first:
is_armstrong: True for 153, 370, 9474; False for 123 and 100.
is_happy: True for 1, 7, 19; False for 2, 4, 20.
is_perfect: True for 6, 28, 496; False for 1, 12 and 27.
Then add n = int(input("Enter a number: ")) and a clear printed message. In the viva, say how you tested it: against numbers you had already checked by hand.
If a check fails and you cannot see why, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer. If the gap is the idea itself, the Fermi Concept Library has a lesson for every concept in mathematics and computing.
Next, see how Python runs your code compared with C; in Class 12, small functions like these are where recursion in Python starts.



