JavaScript Program for Armstrong Numbers
Last Updated :
03 Jan, 2024
In this article, we will see a JavaScript program to check whether the given number is an Armstrong number or not.
An Armstrong Number is an n-digit number that is the sum of the nth power of its all digits. For instance, Consider a 3-digit number, i.e., 153, which is a 3-digit number, & the sum of the cube of all its digits will be the number itself, i.e. 153.
13 = 1
53 = 5*5*5 = 125
33 = 3*3*3 = 27
13 + 53 + 33 = 1+125+27 = 153
To generalize it to a particular syntax form, then the following syntax will be used:
abcd… = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n)+....
Here a,b,c,d,... denotes the Base number & n denotes the exponent number.
Several methods can be used to check if a number is an Armstrong number or not, which are listed below:
In this method, we can convert the given input number to a string using toString() and then an array using split() method to get the individual digits of the number. Then reduce() method iterates over each digit and accumulates the sum using the acc parameter. It uses Math.pow() method to raise each digit to the power of order. After calculating the sum, the function compares it with the original number. If the sum is equal to the number, it prints a message indicating that it is an Armstrong number.
Syntax:
// Syntax for toString() Method
obj.toString()// Syntax for split() Method
str.split(separator, limit)
Example: In this example, we are using the toString() method & the split() method to check the Armstrong number.
JavaScript
function isArmstrong(number) {
const digits = number.toString().split('');
const order = digits.length;
const sum = digits.reduce(
(acc, digit) =>
acc + Math.pow(parseInt(digit), order), 0);
if (sum === number) {
console.log(
number + " is an Armstrong Number");
}
else {
console.log
(number + " is not an Armstrong Number");
}
}
isArmstrong(9474);
isArmstrong(520);
Output9474 is an Armstrong Number
520 is not an Armstrong Number
Approach 2: Using naive Method
The naive approach would be a simple algorithm that repeatedly iterates through a set of numbers and tests each one to see if it is an Armstrong number. In this approach, a loop is used to get the digits of the number and then the sum is calculated as the sum of the nth power of each digit.
Example: This example implements the naive Method for verifying the Armstrong number.
JavaScript
function isArmstrong(number) {
let temp = number;
let o = order(temp)
let sum = 0;
// Loop until temp is greater than 0
while (temp) {
remainder = temp % 10;
// Floor value of the quotient
temp = Math.floor(temp / 10);
sum = sum + Math.pow(remainder, o);
}
if (sum === number) {
console.log(number + " is an Armstrong Number");
}
else {
console.log(number + " is Not an Armstrong Number");
}
}
// Function to calculate number of digits
function order(number) {
let n = 0;
while (number > 0) {
n++;
number = Math.floor(number / 10);
}
return n;
}
// Input value 153
isArmstrong(153);
// Input value 520
isArmstrong(520);
Output153 is an Armstrong Number
520 is Not an Armstrong Number
We can also get the digits using Array.from() method that converts the object into an array as shown below. By utilizing this method, you can get the same result without explicitly using the toString() and split() functions.
Syntax:
Array.from(object, mapFunction, thisValue);
Example: Here we are using the above-explained method.
JavaScript
function isArmstrong(number) {
const digits = Array.from(String(number), Number);
const order = digits.length;
// Calculate the total sum using array.map()
const sum = digits.reduce(
(acc, digit) =>
acc + Math.pow(parseInt(digit), order), 0);
if (sum === number) {
console.log(
number + " is an Armstrong Number");
}
else {
console.log(
number + " is not an Armstrong Number");
}
}
// Input number 1634
isArmstrong(1634);
// Input number 749
isArmstrong(749);
Output1634 is an Armstrong Number
749 is not an Armstrong Number
- Converts the number to a string.
- Uses the
reduce
method on the array of digits to calculate the sum. - Raises each digit to the power of the number of digits.
- Checks if the sum is equal to the original number.
Example: In this example, we are using Array Reduce() method.
JavaScript
function isArmstrong(number) {
const numStr = number.toString();
const numDigits = numStr.length;
const sum = [...numStr].reduce((acc, digit) =>
acc + Math.pow(parseInt(digit), numDigits), 0);
return sum === number;
}
console.log(isArmstrong(153)); // Example usage
Similar Reads
JavaScript Program to Display Armstrong Numbers Between 1 to 1000
Armstrong number is a number equal to the sum of its digits raised to the power 3 of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. This article will explore how to find Armstrong numbers between 1 to 1000 using JavaScript. Approach 1: Display Armstrong
2 min read
JavaScript Program to Display Armstrong Number Between Two Intervals
An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. In this article, we'll explore how to create a JavaScript program to display all Armstrong numbers within a
2 min read
JavaScript Numbers Coding Practice Problems
Numbers are fundamental in JavaScript, used for calculations, data analysis, and algorithm implementation. This curated list of JavaScript number practice problems covers essential concepts to master JavaScript Numbers. Whether you're a beginner or looking to sharpen your numerical computation skill
1 min read
Javascript Program To Write Your Own atoi()
The atoi() function in C takes a string (which represents an integer) as an argument and returns its value of type int. So basically the function is used to convert a string argument to an integer. Syntax: int atoi(const char strn) Parameters: The function accepts one parameter strn which refers to
5 min read
PHP | Check if a number is armstrong number
Given a number, we need to check whether it is an armstrong number or not in PHP. An Armstrong number is the one whose value is equal to the sum of the cubes of its digits.Examples: Input : 407 Output : Yes 407 = (4*4*4) + (0*0*0) + (7*7*7) = 64 + 0 + 343 = 407 Input : 303 Output : No Approach: For
2 min read
JavaScript Programs
JavaScript Programs contains a list of articles based on programming. This article contains a wide collection of programming articles based on Numbers, Maths, Arrays, Strings, etc., that are mostly asked in interviews.Table of ContentJavaScript Basic ProgramsJavaScript Number ProgramsJavaScript Math
13 min read
JavaScript Program to Print Floydâs Pattern Triangle Pyramid
Floyd's Triangle is a right-angled triangular array of natural numbers, used primarily in computer science education. It is named after Robert Floyd. Each row of the triangle corresponds to the sequence of consecutive natural numbers. In this article, we will explore different approaches to implemen
2 min read
Java Program to check Armstrong Number
Given a number x. Write a Java Program to determine whether the given number is Armstrong's number or not. In this article, we will learn how to check Armstrong Numbers in Java. What is Armstrong's Number?In a mathematical number system, the Armstrong number is the number in any given number base, w
4 min read
C Program To Find Armstrong Numbers Between 1 to 1000
Prerequisites: Program for Armstrong Numbers A positive integer of n digits is called Armstrong number of order n (order is the number of digits) if xyz⦠= pow(x,n) + pow(y,n) + pow(z,n) + â¦. Here we will build a C Program to print Armstrong numbers between 1 to 1000 Input: lowerlimit = 1 higherlimi
2 min read
Armstrong Numbers between two integers
A positive integer with digits a, b, c, d... is called an Armstrong number of order n if following condition is satisfied. abcd... = an + bn + cn + dn +... 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153 Therefore, 153 is an Armstrong number. Examples: Input : 100 400 Output :153 370 371 Explanatio
6 min read