ByteBoard Notes Notes

Determine whether a number is a perfect number, an Armstrong number or a palindrome

Computer Science XI · Experiment 11 of 27 · Python

★ CBSE suggested11-CSPython programs
Expt. No.11
Page No.22

Perfect, Armstrong and palindrome number

Aim

To write a Python program that checks whether a number is a perfect number, an Armstrong number and a palindrome.

Concepts used

Algorithm

  1. Input a number.
  2. Perfect: add all divisors from 1 to number−1; it is perfect if the sum equals the number.
  3. Armstrong: count the digits; add each digit raised to that count; it is Armstrong if the sum equals the number.
  4. Palindrome: reverse the digits using % 10 and // 10; it is a palindrome if the reverse equals the number.
  5. Display all three results.

Program

# Check for perfect, Armstrong and palindrome numbers num = int(input("Enter a number: ")) # Perfect number: sum of proper divisors equals the numbertotal = 0for i in range(1, num):    if num % i == 0:        total = total + iif total == num:    print(num, "is a perfect number")else:    print(num, "is not a perfect number") # Armstrong number: sum of digits raised to the number of digitsdigits = len(str(num))temp = numarm = 0while temp > 0:    d = temp % 10    arm = arm + d ** digits    temp = temp // 10if arm == num:    print(num, "is an Armstrong number")else:    print(num, "is not an Armstrong number") # Palindrome: number reads the same backwardstemp = numrev = 0while temp > 0:    rev = rev * 10 + temp % 10    temp = temp // 10if rev == num:    print(num, "is a palindrome")else:    print(num, "is not a palindrome")

A marked line continues the line above it. In your file, write it on the same line.

Output

Run 1
$ python perfect_armstrong_palindrome.py
Enter a number: 28 28 is a perfect number 28 is not an Armstrong number 28 is not a palindrome
Run 2
$ python perfect_armstrong_palindrome.py
Enter a number: 153 153 is not a perfect number 153 is an Armstrong number 153 is not a palindrome
Run 3
$ python perfect_armstrong_palindrome.py
Enter a number: 121 121 is not a perfect number 121 is not an Armstrong number 121 is a palindrome

✓ Printed by running this program, not typed by hand.

Result

28 was a perfect number (1+2+4+7+14 = 28), 153 an Armstrong number (1³+5³+3³ = 153) and 121 a palindrome. Each failed the other two tests.

Viva voce: what the examiner may ask

01What do % 10 and // 10 do to a number?

% 10 gives the last digit; // 10 removes the last digit. Together they take a number apart digit by digit.

02Why is temp used instead of num in the loops?

The loops reduce the value to 0. Working on a copy keeps the original number for the final comparison.

03How is the number of digits found?

len(str(num)) converts the number to a string and counts its characters.

04Name another perfect number.

6 (1 + 2 + 3) or 496.

Variations for the lab test

  1. Print all Armstrong numbers between 100 and 999.
  2. Print all perfect numbers below 1000.
  3. Check whether a string, not a number, is a palindrome.
! Common mistake Using num itself in the Armstrong loop: after the loop num is 0, so the comparison arm == num is always false.
Back to the 11-CS index