-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstrongNumber.java
More file actions
29 lines (28 loc) · 931 Bytes
/
ArmstrongNumber.java
File metadata and controls
29 lines (28 loc) · 931 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 BasicPrograms;
/**
An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
* @author Srinvas Vadige, srinivas.vadige@gmail.com
* @since 21 Sept 2024
*/
public class ArmstrongNumber {
public static void main(String[] args) {
int n = 9474;
System.out.println(isArmstrong(n)? "yes" : "no");
}
/**
*
* @param n
* @return
*/
static boolean isArmstrong(int n) {
int sum = 0;
int orgNum = n;
int noOfDigits = (int) Math.log10(n) + 1; // or String.valueOf(num).length();
while (n>0) {
int lastDigit = n%10; // modulus returns remainder
n = n/10; // division returns quotient
sum += (int) Math.pow(lastDigit, noOfDigits); // in java ^ is bitwise XOR operator and ** don't work as power like js
}
return sum == orgNum;
}
}