The number 153 has a surprising property: 1³ + 5³ + 3³ equals 153 itself. Numbers of this kind are called Armstrong numbers. This topic also treats the factorial, a product that appears throughout mathematics and that was already met in the Taylor series.
Armstrong Numbers
Definition: Armstrong number. A number that is equal to the sum of its own digits, each raised to the power of the number of digits.
For a three-digit number, each digit is cubed. For a four-digit number, each digit is raised to the fourth power. Table 3.5 gives examples.
Table 3.5: Testing numbers for the Armstrong property
| Number | Digits | Calculation | Result | Armstrong? |
|---|---|---|---|---|
| 153 | 3 | 1³ + 5³ + 3³ = 1 + 125 + 27 | 153 | Yes |
| 370 | 3 | 3³ + 7³ + 0³ = 27 + 343 + 0 | 370 | Yes |
| 123 | 3 | 1³ + 2³ + 3³ = 1 + 8 + 27 | 36 | No |
| 1634 | 4 | 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1 + 1296 + 81 + 256 | 1634 | Yes |
The three-digit Armstrong numbers are 153, 370, 371 and 407.
Method for the Armstrong Test
The test combines three ideas already studied.
- Count the digits. Using a copy of the number, divide by 10 until it becomes 0 and count the passes. Call the count k.
- Sum the powers. Using another copy, extract each digit, raise it to the power k by repeated multiplication and add the result to a sum.
- Compare. The number is an Armstrong number if the sum equals the original.
The power is found by a small inner loop that multiplies the digit by itself k times. This keeps the whole calculation in integers.
Factorial
Definition: Factorial. The product of all positive integers from 1 to n, written n!, with 0! defined as 1.
Thus 5! = 5 × 4 × 3 × 2 × 1 = 120. The factorial of a negative number is not defined.
Computing a Factorial
A factorial is computed with a running product. The product starts at 1, not 0, because multiplying by 0 would make every result 0.
When n is 0, the loop makes no pass and fact remains 1, which is the correct value of 0!.
Growth of Factorials
Factorials grow extremely fast: 10! is 3,628,800 and 13! is 6,227,020,800. A 32-bit int can hold values only up to 2,147,483,647, so 13! does not fit in it. The largest factorial that an int of this size can hold is 12!. A larger type such as long long extends the range, but only as far as 20!.
Example 3.5: Armstrong test and factorial
Problem: Read a positive integer n. Report whether it is an Armstrong number, and display the factorial of its number of digits.
Solution:
- Count the digits of n; call the count k.
- For each digit, compute digit to the power k with an inner loop and add it to sum.
- Compare sum with n, then compute k! with a running product.
#include <stdio.h>
int main(void)
{
int n, temp, digit, power, i, k = 0, sum = 0;
int fact = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (temp = n; temp > 0; temp = temp / 10)
{
k++;
}
for (temp = n; temp > 0; temp = temp / 10)
{
digit = temp % 10;
power = 1;
for (i = 1; i <= k; i++)
{
power = power * digit;
}
sum = sum + power;
}
if (sum == n)
{
printf("\n%d is an Armstrong number\n", n);
}
else
{
printf("\n%d is not an Armstrong number\n", n);
}
for (i = 1; i <= k; i++)
{
fact = fact * i;
}
printf("Factorial of %d = %d\n", k, fact);
return 0;
}
Input:
153
Output:
Enter a positive integer:
153 is an Armstrong number
Factorial of 3 = 6
Result: 153 has 3 digits, and 1 + 125 + 27 = 153, so it is an Armstrong number. The factorial of 3 is 3 × 2 × 1 = 6.
Common mistakes
- Always cubing the digits, which is correct only for three-digit numbers.
- Starting the factorial product at 0 instead of 1.
- Computing a large factorial in an
intand trusting the overflowed result.
Key takeaways
- An Armstrong number equals the sum of its digits, each raised to the number of digits.
- 153, 370, 371 and 407 are the three-digit Armstrong numbers.
- n! is the product 1 × 2 × … × n, and 0! is 1.
- Factorials grow very fast; 13! is already too large for a 32-bit
int.
Practice questions
- Is 371 an Armstrong number?
- What is the value of 6!?
- Why does the factorial product start at 1?
Answers to the practice questions
- Yes. 3³ + 7³ + 1³ = 27 + 343 + 1 = 371.
-
- Starting at 0 would make every product 0, and 1 leaves the first multiplication unchanged; it also gives 0! = 1 correctly.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit III
