Given an integer n, return true if and only if it is an Armstrong number.

The k-digit number n is an Armstrong number if and only if the kth power of each digit sums to n.

Example 1:

Input: n = 153 Output: true Explanation: 153 is a 3-digit number, and 153 = 13 + 53 + 33.

Example 2:

Input: n = 123 Output: false Explanation: 123 is a 3-digit number, and 123 != 13 + 23 + 33 = 36.

Constraints:

  • 1 <= n <= 108

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program armstrong_numbers
    implicit none

    integer :: n, k, i, sum
    logical :: is_armstrong

    ! Example 1:
    n = 153
    call check(is_armstrong(n), .true.)

    ! Example 2:
    n = 123
    call check(is_armstrong(n), .false.)

contains

    function is_armstrong(n) result(res)
        implicit none
        integer, value :: n
        integer :: k, i, sum
        logical :: res

        k = floor(log10(real(n))) + 1
        sum = 0
        do i = 1, k
            sum = sum + (mod(n, 10)**i)
            n = n / 10
        end do
        res = sum == n
    end function is_armstrong

    subroutine check(actual, expected)
        implicit none
        logical, value :: actual, expected

        if (actual .eqv. expected) then
            write (*, '(A)') "PASSED"
        else
            write (*, '(A)') "FAILED"
        end if
    end subroutine check
end program armstrong_numbers
❌ Compiled
❌ Executed
❌ Correct
```
program armstrong_numbers
    implicit none

    integer :: n, k, i, sum
    logical :: is_armstrong

    ! read input
    read *, n

    ! check if n is an Armstrong number
    is_armstrong = .false.
    k = floor(log10(real(n))) + 1
    do i = 1, k
        sum = 0
        do while (n > 0)
            sum = sum + (mod(n, 10) ** i)
            n = n / 10
        end do
        if (sum == n) then
            is_armstrong = .true.
            exit
        end if
    end do

    ! output result
    if (is_armstrong) then
        write (*, '(A)') "Yes"
    else
        write (*, '(A)') "No"
    end if

end program armstrong_numbers
```
🌐 Data from online sources
def is_armstrong(n: int) -> bool:
    power_sum = 0
    original = n
    k = len(str(n))
    while n > 0:
        digit = n % 10
        power_sum += digit ** k
        n //= 10
    return power_sum == original
  1. Initialize power_sum to 0 and store the input number into a variable called original.
  2. Calculate the length of the number (number of digits) and store it in variable k.
  3. Use a while loop to iterate through each digit of the number.
  4. In each iteration, extract the last digit of the number.
  5. Add the kth power of the digit to power_sum.
  6. Remove the last digit from the number.
  7. After the loop, check if power_sum is equal to the original number. If it is, then the number is an Armstrong number, so return true, otherwise return false.
🌐 Data from online sources
bool isArmstrong(int n) {
    int power_sum = 0, original = n;
    int k = to_string(n).length();
    while (n > 0) {
        int digit = n % 10;
        power_sum += pow(digit, k);
        n /= 10;
    }
    return power_sum == original;
}
  1. Initialize power_sum to 0 and store the input number into a variable called original.
  2. Calculate the length of the number (number of digits) and store it in variable k.
  3. Use a while loop to iterate through each digit of the number.
  4. In each iteration, extract the last digit of the number.
  5. Add the kth power of the digit to power_sum.
  6. Remove the last digit from the number.
  7. After the loop, check if power_sum is equal to the original number. If it is, then the number is an Armstrong number, so return true, otherwise return false.