A perfect, Armstrong and palindrome number program is a favourite question in school practicals, board exams and coding interviews. You are given a number and you have to decide whether it is a perfect number, an Armstrong number or a palindrome. In this guide you will learn how to write the perfect number program, the Armstrong number program and the palindrome number program in Python, C and Java, with simple logic, dry runs, tested output and a free interactive tool that shows the loop working step by step.
Enter a number, pick a check, and watch the loop work step by step.
Graphical view
Code
Output
What You Will Learn
- The problem statement
- What perfect, Armstrong and palindrome numbers are
- The digit trick: % 10 and // 10
- Perfect number program
- Armstrong number program
- Palindrome number program
- One menu-driven program for all three
- Shorter and faster ways to write them
- Numbers to test your program with
- Common mistakes, practice questions and FAQ
Problem Statement
Write a program to determine whether a number is a perfect number, an Armstrong number or a palindrome.
The user enters one whole number. The program reads it, applies the rule of the chosen type, and prints whether the number belongs to that type. The programs in this post work for numbers from 1 up to 9,999,999.
What Are Perfect, Armstrong and Palindrome Numbers?
| Type | Rule | Example | Why it works |
|---|---|---|---|
| Perfect number | The number equals the sum of its divisors, not counting the number itself | 28 | 1 + 2 + 4 + 7 + 14 = 28 |
| Armstrong number | The number equals the sum of its digits, each raised to the power of the number of digits | 153 | 13 + 53 + 33 = 1 + 125 + 27 = 153 |
| Palindrome number | The number reads the same forwards and backwards | 121 | Reversed, 121 is still 121 |
An Armstrong number is also called a narcissistic number. Every single-digit number is an Armstrong number, because a digit raised to the power 1 is the digit itself.
The Digit Trick: % 10 and // 10
Armstrong and palindrome programs both need to take a number apart digit by digit. Two operations do all the work:
temp % 10gives the last digit of temp.temp // 10(ortemp / 10with int variables in C and Java) removes the last digit.
Repeat both steps in a loop until temp becomes 0, and you have visited every digit from right to left. Here is how it looks for 153:
| temp | temp % 10 (last digit) | temp // 10 (digit removed) |
|---|---|---|
| 153 | 3 | 15 |
| 15 | 5 | 1 |
| 1 | 1 | 0 |
Always copy the number into a second variable, called temp in these programs, and destroy that copy. You still need the original n at the end for the final comparison.
Perfect Number Program
To find out whether n is perfect, add up all its divisors that are smaller than n and compare the total with n. A divisor of n is any i for which n % i == 0.
We only need to check i from 1 to n / 2. Any number bigger than n / 2 (other than n itself) cannot divide n evenly, so checking beyond that is wasted work.
Logic:
- Start with
total = 0. - Loop
ifrom 1 to n / 2. - If
n % i == 0, add i to total. - After the loop, if
total == nthe number is perfect. The extra checkn > 0makes sure that 0 is not reported as perfect.
Python program
n = int(input("Enter a number: "))
total = 0
for i in range(1, n // 2 + 1):
if n % i == 0:
total = total + i
if n > 0 and total == n:
print(n, "is a Perfect Number")
else:
print(n, "is not a Perfect Number")
C program
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
int total = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
total = total + i;
}
}
if (n > 0 && total == n) {
printf("%d is a Perfect Number\n", n);
} else {
printf("%d is not a Perfect Number\n", n);
}
return 0;
}
Java program
import java.util.Scanner;
public class NumberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int total = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
total = total + i;
}
}
if (n > 0 && total == n) {
System.out.println(n + " is a Perfect Number");
} else {
System.out.println(n + " is not a Perfect Number");
}
}
}
Sample output:
Enter a number: 28 28 is a Perfect Number
Another run:
Enter a number: 10 10 is not a Perfect Number
Dry run for n = 6:
| i | n % i | Divisor? | total |
|---|---|---|---|
| 1 | 0 | yes, add 1 | 1 |
| 2 | 0 | yes, add 2 | 3 |
| 3 | 0 | yes, add 3 | 6 |
The final total is 6, which is equal to n, so it is a perfect number.
Dry run for n = 10:
| i | n % i | Divisor? | total |
|---|---|---|---|
| 1 | 0 | yes, add 1 | 1 |
| 2 | 0 | yes, add 2 | 3 |
| 3 | 1 | no | 3 |
| 4 | 2 | no | 3 |
| 5 | 0 | yes, add 5 | 8 |
The final total is 8, which is not equal to n, so it is not a perfect number.
Did you know? Perfect numbers are very rare. There are only four below 10,000: 6, 28, 496 and 8128. They follow a pattern found by Euclid: each one equals 2p−1 × (2p − 1), where 2p − 1 is a prime number.
| p | 2^(p−1) × (2^p − 1) | Perfect number |
|---|---|---|
| 2 | 2 × 3 | 6 |
| 3 | 4 × 7 | 28 |
| 5 | 16 × 31 | 496 |
| 7 | 64 × 127 | 8128 |
Armstrong Number Program
For an Armstrong number we first count the digits of n and call that count k. Then every digit is raised to the power k, and all of these powers are added. If the sum equals n, the number is an Armstrong number.
The number of digits matters. For 153, k is 3, so each digit is cubed. For 1634, k is 4, so each digit is raised to the power 4.
Logic:
- Copy n into
temp, count the digits intok, and start withtotal = 0. - While
temp > 0: take the last digit withtemp % 10, adddigit ^ kto total, then remove the digit withtemp // 10. - After the loop, if
total == nthe number is an Armstrong number.
In Python we write the power as digit ** k and count digits with len(str(n)). In C the digits are counted with a small loop, and the power is found with a loop that multiplies digit by itself k times. In Java we use Math.pow() and round the result.
Python program
n = int(input("Enter a number: "))
temp = n
k = len(str(n))
total = 0
while temp > 0:
digit = temp % 10
total = total + digit ** k
temp = temp // 10
if total == n:
print(n, "is an Armstrong Number")
else:
print(n, "is not an Armstrong Number")
C program
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
int temp = n, total = 0, k = 0;
for (int t = n; t > 0; t = t / 10) {
k++;
}
while (temp > 0) {
int digit = temp % 10;
int power = 1;
for (int j = 1; j <= k; j++) {
power = power * digit;
}
total = total + power;
temp = temp / 10;
}
if (total == n) {
printf("%d is an Armstrong Number\n", n);
} else {
printf("%d is not an Armstrong Number\n", n);
}
return 0;
}
Java program
import java.util.Scanner;
public class NumberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int temp = n, total = 0;
int k = String.valueOf(n).length();
while (temp > 0) {
int digit = temp % 10;
total = total + (int) Math.round(Math.pow(digit, k));
temp = temp / 10;
}
if (total == n) {
System.out.println(n + " is an Armstrong Number");
} else {
System.out.println(n + " is not an Armstrong Number");
}
}
}
Sample output:
Enter a number: 153 153 is an Armstrong Number
Another run:
Enter a number: 154 154 is not an Armstrong Number
Dry run for n = 153 (k = 3 digits):
| temp | digit = temp % 10 | digit ^ k | total | temp // 10 |
|---|---|---|---|---|
| 153 | 3 | 3^3 = 27 | 27 | 15 |
| 15 | 5 | 5^3 = 125 | 152 | 1 |
| 1 | 1 | 1^3 = 1 | 153 | 0 |
The final total is 153, which is equal to n, so it is an Armstrong number.
Dry run for n = 154 (k = 3 digits):
| temp | digit = temp % 10 | digit ^ k | total | temp // 10 |
|---|---|---|---|---|
| 154 | 4 | 4^3 = 64 | 64 | 15 |
| 15 | 5 | 5^3 = 125 | 189 | 1 |
| 1 | 1 | 1^3 = 1 | 190 | 0 |
The final total is 190, which is not equal to n, so it is not an Armstrong number.
Palindrome Number Program
A number is a palindrome if reversing its digits gives the same number. We build the reversed number one digit at a time. Each time we take the last digit of temp, we push it onto the end of rev by multiplying rev by 10 and adding the digit.
Logic:
- Copy n into
tempand start withrev = 0. - While
temp > 0:digit = temp % 10, thenrev = rev * 10 + digit, thentemp = temp // 10. - After the loop, if
rev == nthe number is a palindrome.
Python program
n = int(input("Enter a number: "))
temp = n
rev = 0
while temp > 0:
digit = temp % 10
rev = rev * 10 + digit
temp = temp // 10
if rev == n:
print(n, "is a Palindrome")
else:
print(n, "is not a Palindrome")
C program
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
int temp = n, rev = 0;
while (temp > 0) {
int digit = temp % 10;
rev = rev * 10 + digit;
temp = temp / 10;
}
if (rev == n) {
printf("%d is a Palindrome\n", n);
} else {
printf("%d is not a Palindrome\n", n);
}
return 0;
}
Java program
import java.util.Scanner;
public class NumberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int temp = n, rev = 0;
while (temp > 0) {
int digit = temp % 10;
rev = rev * 10 + digit;
temp = temp / 10;
}
if (rev == n) {
System.out.println(n + " is a Palindrome");
} else {
System.out.println(n + " is not a Palindrome");
}
}
}
Sample output:
Enter a number: 12321 12321 is a Palindrome
Another run:
Enter a number: 1231 1231 is not a Palindrome
Dry run for n = 121:
| temp | digit = temp % 10 | rev = rev * 10 + digit | temp // 10 |
|---|---|---|---|
| 121 | 1 | 1 | 12 |
| 12 | 2 | 12 | 1 |
| 1 | 1 | 121 | 0 |
The reversed number is 121, which is equal to n, so it is a palindrome.
Dry run for n = 123:
| temp | digit = temp % 10 | rev = rev * 10 + digit | temp // 10 |
|---|---|---|---|
| 123 | 3 | 3 | 12 |
| 12 | 2 | 32 | 1 |
| 1 | 1 | 321 | 0 |
The reversed number is 321, which is not equal to n, so it is not a palindrome.
Note: a number that ends in 0, such as 10 or 120, can never be a palindrome. Its reverse would start with 0, which disappears when stored as a number, so the reversed value is smaller than n.
One Menu-Driven Program for All Three
In practicals you are often asked to put all three checks in a single program and let the user choose. The program below reads the number, shows a menu, reads the choice, and runs the matching check. Any other choice prints “Invalid choice”.
Python menu program
n = int(input("Enter a number: "))
print("1. Perfect Number")
print("2. Armstrong Number")
print("3. Palindrome Number")
choice = int(input("Enter your choice: "))
if choice == 1:
total = 0
for i in range(1, n // 2 + 1):
if n % i == 0:
total = total + i
if n > 0 and total == n:
print(n, "is a Perfect Number")
else:
print(n, "is not a Perfect Number")
elif choice == 2:
temp = n
k = len(str(n))
total = 0
while temp > 0:
digit = temp % 10
total = total + digit ** k
temp = temp // 10
if total == n:
print(n, "is an Armstrong Number")
else:
print(n, "is not an Armstrong Number")
elif choice == 3:
temp = n
rev = 0
while temp > 0:
digit = temp % 10
rev = rev * 10 + digit
temp = temp // 10
if rev == n:
print(n, "is a Palindrome")
else:
print(n, "is not a Palindrome")
else:
print("Invalid choice")
C menu program
#include <stdio.h>
int main() {
int n, choice;
printf("Enter a number: ");
scanf("%d", &n);
printf("1. Perfect Number\n");
printf("2. Armstrong Number\n");
printf("3. Palindrome Number\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
int total = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
total = total + i;
}
}
if (n > 0 && total == n) {
printf("%d is a Perfect Number\n", n);
} else {
printf("%d is not a Perfect Number\n", n);
}
} else if (choice == 2) {
int temp = n, total = 0, k = 0;
for (int t = n; t > 0; t = t / 10) {
k++;
}
while (temp > 0) {
int digit = temp % 10;
int power = 1;
for (int j = 1; j <= k; j++) {
power = power * digit;
}
total = total + power;
temp = temp / 10;
}
if (total == n) {
printf("%d is an Armstrong Number\n", n);
} else {
printf("%d is not an Armstrong Number\n", n);
}
} else if (choice == 3) {
int temp = n, rev = 0;
while (temp > 0) {
int digit = temp % 10;
rev = rev * 10 + digit;
temp = temp / 10;
}
if (rev == n) {
printf("%d is a Palindrome\n", n);
} else {
printf("%d is not a Palindrome\n", n);
}
} else {
printf("Invalid choice\n");
}
return 0;
}
Java menu program
import java.util.Scanner;
public class NumberMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
System.out.println("1. Perfect Number");
System.out.println("2. Armstrong Number");
System.out.println("3. Palindrome Number");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
if (choice == 1) {
int total = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
total = total + i;
}
}
if (n > 0 && total == n) {
System.out.println(n + " is a Perfect Number");
} else {
System.out.println(n + " is not a Perfect Number");
}
} else if (choice == 2) {
int temp = n, total = 0;
int k = String.valueOf(n).length();
while (temp > 0) {
int digit = temp % 10;
total = total + (int) Math.round(Math.pow(digit, k));
temp = temp / 10;
}
if (total == n) {
System.out.println(n + " is an Armstrong Number");
} else {
System.out.println(n + " is not an Armstrong Number");
}
} else if (choice == 3) {
int temp = n, rev = 0;
while (temp > 0) {
int digit = temp % 10;
rev = rev * 10 + digit;
temp = temp / 10;
}
if (rev == n) {
System.out.println(n + " is a Palindrome");
} else {
System.out.println(n + " is not a Palindrome");
}
} else {
System.out.println("Invalid choice");
}
}
}
Sample output:
Enter a number: 153 1. Perfect Number 2. Armstrong Number 3. Palindrome Number Enter your choice: 2 153 is an Armstrong Number
Shorter and Faster Ways to Write Them
Method 2: Using functions
Putting every check in its own function makes the code reusable. Each function returns True or False, and the main part of the program simply prints the three answers.
def is_perfect(n):
total = 0
for i in range(1, n // 2 + 1):
if n % i == 0:
total += i
return n > 0 and total == n
def is_armstrong(n):
k = len(str(n))
total = 0
temp = n
while temp > 0:
total += (temp % 10) ** k
temp //= 10
return total == n
def is_palindrome(n):
rev = 0
temp = n
while temp > 0:
rev = rev * 10 + temp % 10
temp //= 10
return rev == n
n = int(input("Enter a number: "))
print("Perfect:", is_perfect(n))
print("Armstrong:", is_armstrong(n))
print("Palindrome:", is_palindrome(n))
Sample output:
Enter a number: 153 Perfect: False Armstrong: True Palindrome: False
Method 3: Faster perfect number check
Divisors always come in pairs. If i divides n, then n / i divides n as well. So we only need to loop while i × i is less than or equal to n, and add both i and n / i every time we find a divisor. For a large n this does about the square root of n steps instead of n / 2 steps.
n = int(input("Enter a number: "))
total = 0
if n > 1:
total = 1
i = 2
while i * i <= n:
if n % i == 0:
total += i
if i != n // i:
total += n // i
i += 1
if n > 0 and total == n:
print(n, "is a Perfect Number")
else:
print(n, "is not a Perfect Number")
With this version the program can check big numbers like 33,550,336 and 8,589,869,056, which are the next two perfect numbers after 8128, in a fraction of a second.
Method 4: Using strings in Python
Python can treat a number as text, which gives very short programs. For a palindrome we compare the text with its reverse, and for an Armstrong number we loop over the characters of the text.
n = int(input("Enter a number: "))
if str(n) == str(n)[::-1]:
print(n, "is a Palindrome")
else:
print(n, "is not a Palindrome")
n = int(input("Enter a number: "))
digits = str(n)
total = sum(int(d) ** len(digits) for d in digits)
if total == n:
print(n, "is an Armstrong Number")
else:
print(n, "is not an Armstrong Number")
Use these short versions to check your answers. In exams, write the loop version, because it shows the logic that the examiner wants to see.
Numbers to Test Your Program With
Armstrong numbers up to 5 digits:
| Digits | Armstrong numbers |
|---|---|
| 1 digit | 1, 2, 3, 4, 5, 6, 7, 8, 9 |
| 3 digits | 153, 370, 371, 407 |
| 4 digits | 1634, 8208, 9474 |
| 5 digits | 54748, 92727, 93084 |
Perfect numbers: 6, 28, 496, 8128, 33550336, 8589869056.
Palindromes: there are 9 one-digit palindromes, 9 two-digit palindromes (11, 22, 33, … 99) and 90 three-digit palindromes (101, 111, 121, … 989, 999). Some bigger examples are 1221, 12321 and 1234321.
Some numbers belong to more than one type. For example, 6 is both a perfect number and a palindrome, and every single-digit number is both an Armstrong number and a palindrome. Try the examples in the interactive tool above to see the three result boxes change.
Common Mistakes
- Changing n inside the loop. If you keep dividing n itself, you lose the original value and cannot compare at the end. Always work on a copy called temp.
- Using pow() in C without care. pow() returns a double and can give 124.99999 instead of 125, which then becomes 124 when stored in an int. It also needs the math library on some compilers. Multiply in a loop, as shown above, or round the result.
- Using the wrong power in Armstrong numbers. The power is the number of digits, not always 3. Count the digits first.
- Looping up to n instead of n / 2 in the perfect number program. If you include n itself, the total becomes too big by n, so even a perfect number like 6 fails (1 + 2 + 3 + 6 = 12, not 6). Stop at n / 2.
- Forgetting that 0 is a special case. The perfect number loop does not run for n = 0, so total stays 0 and 0 == 0 looks true. That is why the programs check n > 0.
- Integer division in Python. Use
//to remove the last digit. With a single/Python gives a decimal number such as 15.3, and the program gives a wrong answer after hundreds of extra loop passes.
Practice Questions
- Print all perfect numbers between 1 and 10,000. Hint: put the perfect number check inside another loop.
- Print all Armstrong numbers between 1 and 1000.
- Print all three-digit palindrome numbers.
- Check whether a number is a strong number: the sum of the factorials of its digits equals the number (for example 145 = 1! + 4! + 5!).
- Check whether a number is an abundant number: the sum of its divisors is greater than the number (for example 12).
- Check whether a word, such as “madam”, is a palindrome. Hint: compare the first and last character, then move inwards.
Frequently Asked Questions
What is a perfect number?
A perfect number is a number that is equal to the sum of all its divisors except itself. For example, 28 is perfect because 1 + 2 + 4 + 7 + 14 = 28.
What is an Armstrong number?
An Armstrong number is a number that equals the sum of its digits, each raised to the power of the number of digits. For example, 153 is an Armstrong number because 13 + 53 + 33 = 153.
What is a palindrome number?
A palindrome number is a number that stays the same when its digits are reversed. Examples are 121, 1331 and 12321.
How do I check an Armstrong number in Python?
Count the digits with len(str(n)), then loop through the digits using % 10 and // 10, add each digit raised to that power, and compare the total with the original number.
Why do we loop only up to n / 2 for a perfect number?
The largest divisor of n that is smaller than n is at most n / 2. So no number between n / 2 and n can divide n, and checking them would only waste time.
Is 1 a perfect number?
No. The number 1 has no divisor smaller than itself, so the sum is 0, which is not equal to 1. The smallest perfect number is 6.
Is 0 an Armstrong number or a palindrome?
By the usual definition, 0 is both. In practice, programs for this exercise are written for positive numbers, and a perfect number check should always reject 0.
Can a negative number be a palindrome?
By convention, no. The minus sign would end up at the wrong end after reversing, so negative numbers are not treated as palindromes.
What is the time complexity of these programs?
The Armstrong and palindrome programs run once per digit, so their time complexity is O(d) where d is the number of digits. The simple perfect number program runs n / 2 times, so it is O(n). The faster version with i × i ≤ n takes about O(√n) time.