Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int getSumOfKthPowerOfDigits(int N, int k) {
- // `result` stores the result of sum of k'th power of each digit.
- int result = 0;
- // Run until N is not 0
- while(N != 0) {
- // Modulo 10 gives us the last digit
- // Add digit ^ k to the result
- result += pow(N % 10, k);
- // Remove the last digit.
- N /= 10;
- }
- return result;
- }
- bool isArmstrong(int N) {
- // Get length of the number by getting floor of log10 and adding 1.
- int length = log10(N) + 1;
- // Return true if Sum of k'th power of digits equals original number.
- return getSumOfKthPowerOfDigits(N, length) == N;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement