-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArmstrongNumber.java
More file actions
45 lines (40 loc) · 1.25 KB
/
ArmstrongNumber.java
File metadata and controls
45 lines (40 loc) · 1.25 KB
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
/**
* Armstrong Number Check
* Determines whether a given number is an Armstrong number or not.
* An Armstrong number is a number that is equal to the sum of cubes (or power
* of its length) of its digits.
*/
class armstrong_number {
/**
* Time Complexity: O(log N) where N is the number, corresponding to the number
* of digits in N.
* Space Complexity: O(1)
*/
static boolean armstrongNumber(int n) {
int og_no = n; // Keep original number to compare later
int count = 0;
int temp = n;
// Count the number of digits
while (temp != 0) {
count++;
temp = temp / 10;
}
int sum = 0;
// Calculate the sum of the digits raised to the power of `count`
while (n != 0) {
int digit = n % 10;
sum += Math.pow(digit, count);
n /= 10;
}
// Return true if the sum equals the original number
return (sum == og_no);
}
public static void main(String args[]) {
int n1 = 152;
if (armstrongNumber(n1)) {
System.out.println("Yes, it is an Armstrong Number\n");
} else {
System.out.println("No, it is not an Armstrong Number\n");
}
}
}