MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / ArmstrongNum

Class ArmstrongNum

Java/Armstrong-Number.java:10–52  ·  view source on GitHub ↗

1134. Armstrong Number The k-digit number N is an Armstrong number if and only if the kth power of each digit sums to N. Given a positive integer N, return true if and only if it is an Armstrong number.

Source from the content-addressed store, hash-verified

8 *
9 */
10class ArmstrongNum {
11
12 /* driver function commented
13 public static void main(String[] args) {
14
15 Scanner kb = new Scanner(System.in);
16 int N = kb.nextInt();
17
18 int k = length(N);
19
20 System.out.println(isArmstrongNumber(N, k));
21
22 kb.close();
23 }
24 */
25
26 public static int length(int N) {
27 int length = 0;
28
29 while (N != 0) {
30 N = N / 10;
31 length++;
32 }
33
34 return length;
35 }
36
37 public static boolean isArmstrongNumber(int N, int k) {
38 long powerSum = 0;
39 int n = N;
40
41 while (n != 0) {
42 int rem = n % 10;
43
44 powerSum += (int) Math.pow(rem, k);
45
46 n /= 10;
47 }
48 System.out.println(powerSum);
49 return (powerSum==N);
50 }
51
52}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected