forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArmstrongNumber.java
49 lines (45 loc) · 975 Bytes
/
ArmstrongNumber.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
import java.util.*;
public class ArmstrongNumber
{
static int length_number(int num){
return String.valueOf(num).length();
}
static boolean isArmstrong(int x){
int res = x;
int val = 0;
int len = length_number(x);
while(res>0){
int temp = res % 10;
val += Math.pow(temp, len);
res = res / 10;
}
if(x == val)
return true;
else
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = sc.nextInt();
if(isArmstrong(num))
System.out.println(num+" is an Armstrong Number.");
else
System.out.println(num+" is not an Armstrong Number.");
}
}
/*
Sample input and output
Example 1:
Enter a number : 1002
Number is not an armstrong number.
Example 2:
Enter a number : 153
Number is an armstrong number.
Example 3:
Enter a number : 8208
8208 is an armstrong number.
Complexities
time: O(log(n))
space: O(1)
*/