forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Armstrong-Number.java
52 lines (38 loc) · 900 Bytes
/
Armstrong-Number.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
/**
*
* 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.
*
*/
class ArmstrongNum {
/* driver function commented
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int N = kb.nextInt();
int k = length(N);
System.out.println(isArmstrongNumber(N, k));
kb.close();
}
*/
public static int length(int N) {
int length = 0;
while (N != 0) {
N = N / 10;
length++;
}
return length;
}
public static boolean isArmstrongNumber(int N, int k) {
long powerSum = 0;
int n = N;
while (n != 0) {
int rem = n % 10;
powerSum += (int) Math.pow(rem, k);
n /= 10;
}
System.out.println(powerSum);
return (powerSum==N);
}
}