This tutorial shows to find if a number is Armstrong or not in JavaScript. In JavaScript, we can use while loop to check if a number is an Armstrong number or not.
An integer is an Armstrong number if its value is equal to the total of the number, when each of its digits is raised to the power of the number of digits in the number.
abc... = an + bn + cn + ...
For Example:
153 = 1*1*1 + 5*5*5 + 3*3*3 = 153
Example 1: Check If a Number is Armstrong or Not
// program to check an Armstrong number
function CheckArmstrong(num){
let sum = 0;
let temp = num;
while (temp > 0) {
// finding the one's digit
let remainder = temp % 10;
sum = sum + remainder * remainder * remainder;
// removing last digit from the number and convert into integer
temp = parseInt(temp / 10);
}
if (sum == num) {
return `${num} is an Armstrong number`
}
else {
return `${num} is not an Armstrong number`
}
}
console.log(CheckArmstrong(153))
console.log(CheckArmstrong(222))
Output
153 is an Armstrong number
222 is not an Armstrong number