Write a JavaScript block aiming to find and present the Armstrong numbers of 3 digits. An Armstrong
Question:
Write a JavaScript block aiming to find and present the Armstrong numbers of 3 digits. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3 +7 +1 = 371. Flowchart
Sol71:
Here is a JavaScript code block that finds and presents Armstrong numbers of 3 digits:
// Loop through all 3-digit numbers
for (let i = 100; i =>
let sum = 0;
let num = i;
// Calculate the sum of the cubes of the digits
while (num > 0) {
let digit = num % 10;
sum += digit ** 3;
num = Math.floor(num / 10);
}
// Check if the number is an Armstrong number
if (sum === i) {
console.log(i + " is an Armstrong number!");
}
}
This code block uses a for
loop to iterate through all 3-digit numbers (100 to 999). For each number, it calculates the sum of the cubes of its digits using a while
loop. If the sum is equal to the original number, it is an Armstrong number, and the code block prints a message to the console.