nikunjsoni

1134

Jun 29th, 2021
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     int getSumOfKthPowerOfDigits(int N, int k) {
  4.        // `result` stores the result of sum of k'th power of each digit.
  5.        int result = 0;
  6.  
  7.        // Run until N is not 0
  8.        while(N != 0) {
  9.            // Modulo 10 gives us the last digit
  10.            // Add digit ^ k to the result
  11.            result += pow(N % 10, k);
  12.  
  13.            // Remove the last digit.
  14.            N /= 10;
  15.        }
  16.        return result;
  17.     }
  18.     bool isArmstrong(int N) {
  19.         // Get length of the number by getting floor of log10 and adding 1.
  20.         int length = log10(N) + 1;
  21.  
  22.         // Return true if Sum of k'th power of digits equals original number.
  23.         return getSumOfKthPowerOfDigits(N, length) == N;
  24.     }
  25. };
RAW Paste Data