Leetcode Problem 1134. Armstrong Number

1134. Armstrong Number

Leetcode Solutions

Calculate k Without Built-in Methods

  1. Store the original number n in a temporary variable tempN.
  2. Initialize a counter length to 0.
  3. While tempN is not 0, divide tempN by 10 and increment length.
  4. After the loop, length will hold the value of k, the number of digits in n.
  5. Reset tempN to the original number n.
  6. Initialize a variable sum to 0.
  7. While tempN is not 0, calculate the last digit of tempN using tempN % 10, raise it to the power of k, add the result to sum, and then remove the last digit from tempN by dividing it by 10.
  8. After the loop, if sum equals the original number n, return true. Otherwise, return false.
UML Thumbnail

Calculate k by Converting to String

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...