forked from adarshpandey10t/Hacktoberfestmine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharmstrong.js
More file actions
62 lines (55 loc) · 1.11 KB
/
armstrong.js
File metadata and controls
62 lines (55 loc) · 1.11 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
53
54
55
56
57
58
59
60
61
62
function power(x, y)
{
if( y == 0)
return 1;
if (y % 2 == 0)
return power(x, parseInt(y / 2, 10)) *
power(x, parseInt(y / 2, 10));
return x * power(x, parseInt(y / 2, 10)) *
power(x, parseInt(y / 2, 10));
}
// Function to calculate
// order of the number
function order(x)
{
let n = 0;
while (x != 0)
{
n++;
x = parseInt(x / 10, 10);
}
return n;
}
// Function to check whether the
// given number is Armstrong number
// or not
function isArmstrong(x)
{
// Calling order function
let n = order(x);
let temp = x, sum = 0;
while (temp != 0)
{
let r = temp % 10;
sum = sum + power(r, n);
temp = parseInt(temp / 10, 10);
}
// If satisfies Armstrong condition
return (sum == x);
}
let x = 153;
if(isArmstrong(x))
{
document.write("True" + "</br>");
}
else{
document.write("False" + "</br>");
}
x = 1253;
if(isArmstrong(x))
{
document.write("True");
}
else{
document.write("False");
}