forked from dharmanshu1921/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstrong.java
More file actions
Latest commit
52 lines (48 loc) · 1 KB
/
Copy pathArmstrong.java
File metadata and controls
52 lines (48 loc) · 1 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
46
47
48
49
50
51
52
// Java program to determine whether the number is
// Armstrong number or not
publicclassArmstrong {
/* Function to calculate x raised to the
power y */
intpower(intx, longy)
{
if (y == 0)
return1;
if (y % 2 == 0)
returnpower(x, y / 2) * power(x, y / 2);
returnx * power(x, y / 2) * power(x, y / 2);
}
/* Function to calculate order of the number */
intorder(intx)
{
intn = 0;
while (x != 0) {
n++;
x = x / 10;
}
returnn;
}
// Function to check whether the given number is
// Armstrong number or not
booleanisArmstrong(intx)
{
// Calling order function
intn = order(x);
inttemp = x, sum = 0;
while (temp != 0) {
intr = temp % 10;
sum = sum + power(r, n);
temp = temp / 10;
}
// If satisfies Armstrong condition
return (sum == x);
}
// Driver Program
publicstaticvoidmain(String[] args)
{
Armstrongob = newArmstrong();
intx = 153;
System.out.println(ob.isArmstrong(x));
x = 1253;
System.out.println(ob.isArmstrong(x));
}
}