# Finding all Armstrong Numbers in a given range

Get FREE domain for 1st year and build your brand new site

Reading time: 15 minutes | Coding Time: 5 minutes

In this article, we will develop an approach to find all armstrong numbers in a given range. The approach is to check for each number in the range if it is an armstrong number or not.

To find all the Armstrong Numbers in a given range, first we need to know what Armstrong Number is.

**Armstrong Number**, also known as *Narcissistic Number* or a *Plus Perfect Number* of a given number base is a number that is the sum of its own digits each raised to the power of the number of digits.

In other words,a positive integer is called **Armstrong Number** of order *n* if and only if

abcd... = a

^{n}+ b^{n}+ c^{n}+ d^{n}+ ...

# Example

In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example, 153 is an Armstrong number because

$1\times 1\times 1$

+$5\times 5\times 5$+$3\times 3\times 3$ = $1$ + $125$ + $27$ =$153$

In the range 0 to 999 there exists six Armstrong numbers- 0, 1, 153, 370, 371 and 407.

Similarly, in the case of 4 digits, 1634 is an Armstrong Number as,

$1\times 1\times 1\times1$

+$6\times 6\times 6\times 6$+$3\times 3\times 3\times 3$+$4\times 4\times 4\times4$= $1$ + $1296$ + $81$ + $256$ =$1634$

In the range 1000 to 9999 there are three Armstrong numbers- 1634, 8208 and 9474.

# Algorithm

- Accept the range from user.
- Find the total order(number of digits) of the given number.
- For each digit , find digit raised to the power of o where o is the order calculated in Step 2.
- Compare sum of all such values with the given number.
- Print the numbers which satisfy the condition in Step 4.

# Psuedocode for Checking Armstrong Number in a Range

You can learn how to check for armstrong number using this article at OpenGenus.

Following is the function that extends it for a range of numbers (lower to upper):

```
int armstrong(int upper,int lower)
{
for(i=lower+1;i<=upper;i++)
{
temp1=temp2=i;
while(temp1!=0)
{
temp1 /=10; ++num;
}
while(temp2 !=0)
{
rem=temp2 %10;
sum_pow +=pow(rem,num);
temp2 /=10;
}
if((int)sum_pow ==i)
{
return i;
}
num,sum_pow=0;
}
}
```

# Implementations

The code to find all the armstrong numbers in a given range is provided in the following languages:

- Python

Following is the Python implementation of our approach:

```
#Accept lower and upper bound of range from user
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("The armstrong numbers are: ")
#Calculates number of digits
for number in range(lower, upper + 1):
order = len(str(number))
#Computes sum of nth power
sum_pow = 0
temp = number
while temp:
temp,digit = divmod(temp,10)
sum_pow+=digit ** order
# Checks if number is equal to
# the sum of nth power of its digits
if number == sum_pow:
print(number)
```

Sample Output 1:

```
Enter lower range: 100
Enter upper range: 1000
The armstrong numbers are:
153
370
371
407
```

Sample Output 2:

```
Enter lower range: 1000
Enter upper range: 9999
The armstrong numbers are:
1634
8208
9474
```

- C

Following is the C implementation of our approach:

```
#include <math.h>
#include <stdio.h>
int main()
{
int lower, upper, i, temp1, temp2, rem, num = 0;
float sum_pow = 0.0;
/* Accept the lower and upper range from the user */
printf("Enter lower range: ");
scanf("%d", &lower);
printf("Enter upper range: ");
scanf("%d", &upper);
printf("Armstrong numbers between %d and %d are: ", lower, upper);
for (i = lower + 1; i < upper; ++i)
{
temp1 = i;
temp2 = i;
// calculate number of digits
while (temp1 != 0)
{
temp1 /= 10;
++num;
}
// calculate sum of nth power of its digits
while (temp2 != 0) {
rem = temp2 % 10;
sum_pow += pow(rem, num);
temp2 /= 10;
}
// check if it is an Armstrong number
if ((int)sum_pow == i) {
printf("%d ", i);
}
num = 0;
sum_pow = 0;
}
return 0;
}
```

Sample Output 1:

```
Enter lower range: 100
Enter upper range: 1000
Armstrong numbers between 100 and 1000 are: 153 370 371 407
```

Sample Output 2:

```
Enter lower range: 1000
Enter upper range: 9999
Armstrong numbers between 1000 and 9999 are: 1634 8208 9474 */
```

### Complexity

- Worst case time complexity:
**O(n*d)** - Average case time complexity:
**O(n*d)** - Best case time complexity:
**O(n*d)** - Space complexity:
**O(1)**

Time Complexity:

We are going to loop through the given range of numbers that will take O(n) where n is the difference between the given range of numbers, following which we will be checking if the number is Armstrong or not that will take O(d) where d is the number of digits in the biggest number, so Time complexity is O(n * d).

Space Complexity:

We are using constant space, so Space complexity is O(1).

# Applications

It is difficult to transmit data from one place to another in a proper secure manner. To ensure secured data transmission, universal technique called cryptography is used, which provides confidentiality of the transmitted data.

Encryption and decryption process uses Armstrong number which is referred as a secret key. In existing system â€śSecurity Using Colors andArmstrong Numbersâ€ť the first step is to assign a unique color for each receiver. Each color is represented

with a set of Three values in RGB format (238, 58,140). This encrypted color actually acts as a password. The actual data is encrypted using Armstrong numbers.

To know more about application of armstrong number in data security check out this PDF Journal.

## Question:

#### How many armstrong numbers are there in between 1000 and 5000?

Learn more:

- Check if a number is an armstrong number by
**Ashutosh Singh**at OpenGenus - List of Mathematical algorithms at
**OpenGenus**