🔀📚Intermediate

Display Armstrong Numbers Between Two Intervals

Find Armstrong numbers in given range

This program finds all Armstrong numbers between two user-specified intervals.

C Program to Display Armstrong Numbers Between Two Intervals

armstrong_interval.c
C
1#include <stdio.h>
2#include <math.h>
3
4int countDigits(int num) {
5 int count = 0;
6 while (num != 0) {
7 num /= 10;
8 count++;
9 }
10 return count;
11}
12
13int isArmstrong(int num) {
14 int original = num;
15 int n = countDigits(num);
16 int sum = 0;
17
18 while (num != 0) {
19 int digit = num % 10;
20 sum += (int)pow(digit, n);
21 num /= 10;
22 }
23
24 return sum == original;
25}
26
27int main() {
28 int low, high;
29
30 printf("Enter lower limit: ");
31 scanf("%d", &low);
32 printf("Enter upper limit: ");
33 scanf("%d", &high);
34
35 printf("\nArmstrong numbers between %d and %d:\n", low, high);
36
37 int found = 0;
38 for (int i = low; i <= high; i++) {
39 if (isArmstrong(i)) {
40 printf("%d ", i);
41 found = 1;
42 }
43 }
44
45 if (!found) {
46 printf("None found");
47 }
48 printf("\n");
49
50 return 0;
51}
Output

Enter lower limit: 100

Enter upper limit: 500

 

Armstrong numbers between 100 and 500:

153 370 371 407