[LeetCode] Armstrong Number

1134. Armstrong Number

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool isArmstrong(int n) {
int len = to_string(n).length();
int sum = 0, x = n;
while(x) {
sum += pow(x % 10, len);
x /= 10;
}
return sum == n;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/07/30/PS/LeetCode/armstrong-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.