---
title: "Armstrong numbers in a range"
subject: "Computer Programming"
chapter: 6
key: "semester_1/computer_programming:armstrong-range-function"
source: https://www.bctengineer.com/q/semester_1/computer_programming/armstrong-range-function
site: BCT Engineering
---

# Armstrong numbers in a range

Computer Programming · Chapter 6: User-Defined Functions

- Asked in: 2071 Chaitra, 4 marks
- Video: [C Program To Find Armstrong Numbers Between Range using Function](https://www.youtube.com/watch?v=nsVcZxEvIc8) — Technotip
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/armstrong-range-function
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-6

## Question

Write a program to display Armstrong numbers between the range entered by a user and also display their counts. You must use a function to check for Armstrong numbers and display them from main.

## Short answer

The program defines an is_armstrong function that counts digits, computes the sum of each digit raised to that power, and returns 1 if it equals n. main reads lower/upper limits, loops through the range, calls the function, and prints Armstrong numbers along with their count. For 1–1000, output: 1 2 3 4 5 6 7 8 9 153 370 371 407, count=13.

## Answer

The function `is_armstrong(n)` counts the digits of `n`, adds each digit raised to that power, and returns 1 if the sum equals `n`. `main()` reads the limits, loops through the range, calls the function, prints the Armstrong numbers and counts them.

```c
#include <stdio.h>
int is_armstrong(int n)
{
    int t = n, digits = 0, sum = 0, d, p, i;
    while (t > 0) { digits++; t /= 10; }
    t = n;
    while (t > 0)
    {
        d = t % 10;
        p = 1;
        for (i = 0; i < digits; i++)
            p *= d;
        sum += p;
        t /= 10;
    }
    return sum == n;
}
int main(void)
{
    int lo, hi, i, count = 0;
    printf("Enter lower and upper limits: ");
    scanf("%d %d", &lo, &hi);
    printf("Armstrong numbers: ");
    for (i = lo; i <= hi; i++)
        if (is_armstrong(i))
        {
            printf("%d ", i);
            count++;
        }
    printf("\nCount = %d\n", count);
    return 0;
}
```
Output for the range 1 to 1000:
```text
Enter lower and upper limits: Armstrong numbers: 1 2 3 4 5 6 7 8 9 153 370 371 407 
Count = 13
```

The Armstrong numbers between 1 and 1000 are 1 to 9, 153, 370, 371 and 407 (13 in total).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What must is_armstrong(n) find before it can add the digit powers?
   A. The largest digit of n
   B. The square root of n
   C. The number of digits of n
   D. The sum of the digits of n
   Answer: C. The power used is the number of digits, so digits are counted in the first loop. (Key point: Digit count first)

2. How is each digit taken from t in the second loop?
   A. d = t % 10, then t /= 10
   B. d = t / 10, then t %= 10
   C. d = t % 100, then t -= 1
   D. d = t * 10, then t /= 2
   Answer: A. t % 10 gives the last digit and t / 10 drops it. (Key point: Extracting digits)

3. What is the job of main() in this program?
   A. Compute the digit powers itself
   B. Only read the limits and let the function print the numbers
   C. Loop over the range, call the function, print Armstrong numbers and count them
   D. Return the count from is_armstrong
   Answer: C. The question requires the function to only check; display and counting are done in main. (Key point: Role of main)

4. How many Armstrong numbers lie between 1 and 1000?
   A. 13
   B. 9
   C. 4
   D. 12
   Answer: A. They are 1 to 9 (9 numbers) plus 153, 370, 371 and 407, giving 13. (Key point: Result for 1 to 1000)

## Related in Computer Programming

- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6
- [nth Fibonacci term prime check](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afibonacci-nth-prime) — Computer Programming, chapter 6
- [Sum and average until 0 is entered](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-average-until-zero) — Computer Programming, chapter 5
- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [Algorithm for numbers divisible by 9 not 6](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgo-flowchart-divisible-9-not-6) — Computer Programming, chapter 2
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7

## Related in other subjects

- [8086 ALP: vowels to uppercase, counts](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-vowels-uppercase-count) — Microprocessor, chapter 3
- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [8086 ALP: largest and smallest, display](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-largest-smallest-display) — Microprocessor, chapter 3
