-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstrongNumbers.java
More file actions
32 lines (29 loc) · 953 Bytes
/
Copy pathArmstrongNumbers.java
File metadata and controls
32 lines (29 loc) · 953 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
30
31
32
//Check if a number is Armstrong or not.
package codingbat;
import java.util.Scanner;
public class ArmstrongNumbers {
static int sum = 0;
static int ar = 0;
static int armstrongnum = 0;
public static void arm(){
int number = ar % 10;
sum = sum + (int)Math.pow(number,3);
ar = ar/10;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Insert the number to check if it is an Armstrong number or not:");
ar = sc.nextInt();
armstrongnum = ar;
int len = (int)(Math.log10(ar)+1);
for(int i = 0; i < len; i++){
arm();
}
if(sum == armstrongnum){
System.out.println("The number is an armstrong number!");
}
else{
System.out.println("The number is NOT an armstrong number!");
}
}
}