-
Notifications
You must be signed in to change notification settings - Fork 0
/
19. Find_ArmStrong_number_in_an_interval.js
64 lines (58 loc) · 1.51 KB
/
19. Find_ArmStrong_number_in_an_interval.js
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
63
64
//* -------------- Find armstrong number in an interval
//? This program prompts the user to enter a range (start and end) and then prints all Armstrong numbers within that interval.
function findArmStrongInterval(start, end) {
if (
!isNaN(start) &&
!isNaN(end) &&
Number.isInteger(start) &&
Number.isInteger(end) &&
start > 0 &&
start < end
) {
for (let i = start; i <= end; i++) {
let num = i;
let sum = 0;
let temp = num;
while (temp != 0) {
let remainder = temp % 10;
sum = sum + Math.pow(remainder, 3);
temp = Math.floor(temp / 10);
}
if (sum == num) {
console.log(num + " is an Armstrong number");
}
}
} else {
console.log("Invalid input");
}
}
findArmStrongInterval(222, 343);
//* method 2
function findArmStrongInterval(start, end) {
if (
!isNaN(start) &&
!isNaN(end) &&
Number.isInteger(start) &&
Number.isInteger(end) &&
start > 0 &&
start < end
) {
for (let i = start; i <= end; i++) {
let num = i;
let sum = 0;
let temp = num;
let noOfDigits = num.toString().length;
while (temp != 0) {
let remainder = temp % 10;
sum = sum + Math.pow(remainder, noOfDigits);
temp = Math.floor(temp / 10);
}
if (sum === i) {
console.log(i + " is an Armstrong number");
}
}
} else {
console.log("Invalid input");
}
}
findArmStrongInterval(222, 343);