An Armstrong number is a number that is equal to the sum of its digits, each raised to the power of the total number of digits. For example, 153 is an Armstrong number because 1³ + 5³ + 3³ = 153.
- The program checks every number within the given range and prints the Armstrong numbers.
- The range includes both the lower and upper limits.
Examples
Input: Lower limit = 100, Upper limit = 500
Output: 153, 370, 371, 407Input: Lower limit = 1, Upper limit = 1000
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407
Approaches to Display Armstrong Number Between Two Intervals
We can check Armstrong numbers in a given range using the following approaches:
1. Iterative Approach
The iterative approach checks every number in the given range and calculates the sum of the powers of its digits.
Steps
- Set the lower and upper limits of the range.
- Iterate through every number from the lower limit to the upper limit.
- Count the number of digits in the current number.
- Extract each digit and raise it to the power of the number of digits.
- Add the resulting values.
- If the sum is equal to the original number, print it.
#include <stdio.h>
// Function to calculate base raised to exp
int power(int base, int exp)
{
int result = 1;
while (exp--)
result *= base;
return result;
}
// Function to count the number of digits
int countDigits(int num)
{
int count = 0;
while (num != 0) {
count++;
num /= 10;
}
return count;
}
// Function to check whether a number is Armstrong
int isArmstrong(int num)
{
int original = num;
int digits = countDigits(num);
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += power(digit, digits);
num /= 10;
}
return sum == original;
}
int main()
{
int low = 100, high = 500;
printf("Armstrong numbers between %d and %d are:\n",
low, high);
for (int i = low; i <= high; i++) {
if (isArmstrong(i))
printf("%d ", i);
}
return 0;
}
Output
Armstrong numbers between 100 and 500 are: 153 370 371 407
Explanation
- countDigits() counts the digits, while power() calculates the required power.
- isArmstrong() extracts each digit and adds its power to sum.
- The number is printed if sum equals the original number.
- i <= high ensures both limits are included.
2. Approach Using Logarithms
The number of digits can also be calculated using the logarithm formula ⌊log₁₀(n)⌋ + 1. The remaining steps are the same: extract each digit, calculate its power, and compare the sum with the original number.
Steps
- Set the lower and upper limits.
- Iterate through every number in the range.
- Calculate the number of digits using log10().
- Extract each digit using % 10.
- Add each digit raised to the required power.
- Print the number if the calculated sum equals the original number.
#include <stdio.h>
#include <math.h>
// Function to check whether a number is Armstrong
int isArmstrong(int num)
{
int original = num;
int digits = (int)log10(num) + 1;
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += (int)pow(digit, digits);
num /= 10;
}
return sum == original;
}
int main()
{
int low = 100, high = 500;
printf("Armstrong numbers between %d and %d are:\n",
low, high);
for (int i = low; i <= high; i++) {
if (isArmstrong(i))
printf("%d ", i);
}
return 0;
}
Output
Armstrong numbers between 100 and 500 are: 153 370 371 407
Explanation
- isArmstrong() counts the digits using log10() and stores the original number.
- The while loop extracts each digit and adds its required power to sum.
- num /= 10 removes the last digit.
- The for loop checks each number in the given range and prints Armstrong numbers.
Note: The logarithm-based approach requires the math.h library and linking with the math library on some compilers. If you encounter an undefined reference to 'pow' error, compile the program with the appropriate math library option, such as -lm with GCC.