mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Compare commits
3 Commits
fda130b304
...
b75ead90ea
Author | SHA1 | Date | |
---|---|---|---|
|
b75ead90ea | ||
|
d3fe84d023 | ||
|
1718c91ed6 |
22
src/algorithms/math/armstrong.js
Normal file
22
src/algorithms/math/armstrong.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Check Armstrong Number of n Digits
|
||||||
|
|
||||||
|
let number = prompt("Enter a positive integer");
|
||||||
|
let numberOfDigits = number.length;
|
||||||
|
let sum = 0;
|
||||||
|
|
||||||
|
let temp = number;
|
||||||
|
|
||||||
|
while (temp > 0) {
|
||||||
|
|
||||||
|
let remainder = temp % 10;
|
||||||
|
|
||||||
|
sum += remainder ** numberOfDigits;
|
||||||
|
|
||||||
|
temp = parseInt(temp / 10);
|
||||||
|
|
||||||
|
if (sum == number) {
|
||||||
|
console.log(`${number} is an Armstrong number`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log(`${number} is not an Armstrong number.`);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user