# Armstrong Numbers ### Problem Statement

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

Write a program to find all Armstrong number in the range of 0 and 999. ### Solution

```! ---------------------------------------------------------------
! This program computes all Armstrong numbers in the range of
! 0 and 999.  An Armstrong number is a number such that the sum
! of its digits raised to the third power is equal to the number
! itself.  For example, 371 is an Armstrong number, since
! 3**3 + 7**3 + 1**3 = 371.
! ---------------------------------------------------------------

PROGRAM  ArmstrongNumber
IMPLICIT  NONE

INTEGER :: a, b, c                   ! the three digits
INTEGER :: abc, a3b3c3               ! the number and its cubic sum
INTEGER :: Count                     ! a counter

Count = 0
DO a = 0, 9                          ! for the left most digit
DO b = 0, 9                       !   for the middle digit
DO c = 0, 9                    !     for the right most digit
abc    = a*100 + b*10 + c   !        the number
a3b3c3 = a**3 + b**3 + c**3 !        the sum of cubes
IF (abc == a3b3c3) THEN     !        if they are equal
Count = Count + 1        !           count and display it
WRITE(*,*)  'Armstrong number ', Count, ': ', abc
END IF
END DO
END DO
END DO

END PROGRAM  ArmstrongNumber
```
Click here to download this program. ### Program Input and Output

The following is the output from the above program. Thus, there are six Armstrong numbers in the range of 0 and 999.
```Armstrong number 1: 0
Armstrong number 2: 1
Armstrong number 3: 153
Armstrong number 4: 370
Armstrong number 5: 371
Armstrong number 6: 407
``` ### Discussion

• Three-digit numbers are 000, 001, 002, ..., 009, 010, 011, ..., 019, 020, 021, 022, ..., 099, 100, 101, 102, ..., 109, 110, ... 990, 991, ..., 999. As you can see the right-most digits changes faster than the middle one, which in turn is faster than the left-most one. As the left-most and the middle digits are fixed to 0 and 0, the right-most digit changes from 0 to 9. Then, the middle one is increased from 0 to 1. In other words, whenever the right-most digit completes a 0 to 9 cycle, the middle digit is increased by one and the right-most digit restart another 0 to 9 cycle. By the same token, whenever the middle digit completes a 0 to 9 cycle, the left-most digit is increased by 1 and the middle digit restarts another 0 to 9 cycle.

Therefore, if a, b and c are the left-most, the middle and the right-most digits, the above discussion is formalized with the following three nested DO-loops:

```DO a = 0, 9
DO b = 0, 9
DO c = 0, 9
... the number is abc .....
END DO
END DO
END DO
```
• Now, in the inner most DO, the number in hand is abc, where a, b and c are the left-most, middle and the right-most digits. The number itself is of course a*100 + b*10 + c. The sum of the cubes of the digits is a**3 + b**3 + c**3. In the program, these two are stored in abc and a3b3c3, respectively.
• Finally, if abc and a3b3c3 are equal, we have found an Armstrong number. We can use abc or a3b3c3