forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstrong.java
More file actions
29 lines (27 loc) · 799 Bytes
/
Armstrong.java
File metadata and controls
29 lines (27 loc) · 799 Bytes
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
package com.thealgorithms.maths;
/**
* An Armstrong number is equal to the sum of the cubes of its digits. For
* example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370. An
* Armstrong number is often called Narcissistic number.
*
* @author Vivek
*/
public class Armstrong {
/**
* Checks whether a given number is an armstrong number or not.
*
* @param number number to check
* @return {@code true} if given number is armstrong number, {@code false}
* otherwise
*/
public boolean isArmstrong(int number) {
long sum = 0;
long number2 = number;
while (number2 > 0) {
long mod = number2 % 10;
sum += Math.pow(mod, 3);
number2 /= 10;
}
return sum == number;
}
}