C Program to Display Armstrong Numbers Between Two Intervals - User-Defined Range
Display Armstrong Numbers Between Two Intervals
#include <stdio.h>
int main() {
int start, end, number, originalNumber, remainder, result;
printf("Enter the starting point of the interval: ");
scanf("%d", &start);
printf("Enter the ending point of the interval: ");
scanf("%d", &end);
printf("Armstrong numbers between %d and %d are:\n", start, end);
for (number = start; number <= end; ++number) {
originalNumber = number;
result = 0;
// Compute the sum of the cubes of digits
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += remainder * remainder * remainder;
originalNumber /= 10;
}
// If result equals the number, it is an Armstrong number
if (result == number) {
printf("%d\n", number);
}
}
return 0;
}
Enter the starting point of the interval: 100
Enter the ending point of the interval: 500
Armstrong numbers between 100 and 500 are:
153
370
371
407
This program generalizes the search for Armstrong numbers by letting the user specify any start and end values. It follows the same digit‑cubing logic as the previous example and prints each Armstrong number found in the range.
⬅ Previous TopicC Program - Display Armstrong Numbers Between 1 to 1000
Next Topic ⮕C Program - Print Simple Pyramid Pattern
Next Topic ⮕C Program - Print Simple Pyramid Pattern
Comments
Loading comments...