#include <stdio.h>
#include <math.h>
int main() {
int lower, upper, num, originalNum, remainder, n = 0, result = 0;
// Asking the user to enter the lower and upper bounds
printf("Enter the lower bound: ");
scanf("%d", &lower);
printf("Enter the upper bound: ");
scanf("%d", &upper);
// Loop to go through all numbers in the interval
for (num = lower; num <= upper; num++) {
originalNum = num;
result = 0;
n = 0;
// Find the number of digits in the number
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
// Calculate the sum of nth powers of its digits
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
// Check if the number is an Armstrong number
if (result == num) {
printf("%d is an Armstrong number.\n", num);
}
}
return 0;
}
Explanation:
Variables:
lower, upper: Store the user-defined range of numbers.
num: Current number being checked.
originalNum, remainder: Used for calculations on num.
n: Number of digits in num.
result: Stores the sum of the digits raised to the power of n.
User Input:
User enters the lower and upper bounds for the interval.
For Loop:
Loops through each number in the interval.
Digit Count:
Calculates how many digits are in the current number (num).
Sum of Digits Raised to Power n:
For each digit in the number, the program raises it to the power of n and adds the result to result.
Armstrong Check:
If the sum of the digits raised to their powers equals the original number, it’s printed as an Armstrong number.
Output:
Prints Armstrong numbers found between the two intervals.
Output:
Enter the lower bound: 100
Enter the upper bound: 500
153 is an Armstrong number.
370 is an Armstrong number.
371 is an Armstrong number.
407 is an Armstrong number.