-
Notifications
You must be signed in to change notification settings - Fork 0
/
armstrongNumberGenerator.js
47 lines (42 loc) · 1.16 KB
/
armstrongNumberGenerator.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
// A program to print the Armstrong Numbers until a number N //
// let num
// do{
// num = parseInt(prompt("Please enter a number (In the range 0-10000): "))
// }while(checkNumber(num));
const number = 155
console.log("Printing Armstrong numbers from 1-",number," : ")
printArmstrongNums(number)
// function checkNumber(num)
// {
// if(num<0 || !Number.isInteger(num) || num>10000)
// {
// console.log("Invalid Input, please try again!")
// return true;
// }
// else return false;
// }
function isArmstrongNum(number)
{
let sum = 0
const digits = number.toString().length
for(let i=number;i>0;i = Math.floor(i/10))
{
let temp = i%10
sum += temp**digits
}
if(sum === number) return number
else return -1
}
function printArmstrongNums(number)
{
let armstrongNumArray = []
for(let index=0,countArmstrongNums=0;index<number;index++)
{
if(isArmstrongNum(index)!=-1)
{
armstrongNumArray.push(index);
countArmstrongNums++;
}
}
console.log(armstrongNumArray.join(', '))
}