Armstrong Number in JavaScript19 Apr 2025 | 5 min read An Armstrong number is a special integer in which the sum of its digits, raised to the power of the number's length, is equal to the number. For instance, 153 is an Armstrong number since 1^3 + 5^3 + 3^3 = 153. Definition of Armstrong NumberAn Armstrong number is a number in which the sum of cubes of its digits is equal to the number itself. It can also be described as a narcissistic number. A number will be called a narcissistic number or an Armstrong number if the sum of n-th powers of its digits (where n represents the number of digits) equals the number. We can identify whether a provided number is an Armstrong number with the help of a simple JavaScript program. The program will take an integer as input and let us know whether the number is an Armstrong number or not. The subsequent parts will describe how to create such a program in JavaScript. The Logic of Armstrong Number in JavaScriptIn the JavaScript code to test whether a number is an Armstrong number, we will accept an integer. Following the definition of an Armstrong number given above, if the given number has n digits, then it is an Armstrong number when the sum of every digit taken to the nth power is the same as the original number. Thus, the logic to find an Armstrong number will be: We will pull all the digits from the input number using JavaScript loops. We will then take the nth power of each digit pulled and sum them up. If the summed value is the same as the original number put in, the number is an Armstrong number. If the value is not equal to the number entered, the number is not an Armstrong number. As far as time complexity is concerned, checking if an integer 'n' is an Armstrong number takes O(log(n)) time complexity. The flow chart for the JavaScript program that checks if a number is an Armstrong number is given below. ![]() An example of a three-digit number is 153: Thus, 153 is classified as an Armstrong number. Now consider Because this is not equal to the original number, 123 is not an Armstrong number. Only four three-digit Armstrong numbers exist. For all other powers, the value of n will differ. For a four-digit number, the power will be 4. For example, 1634 can be calculated as follows: Here, as we have 4 digits, we have used 4 as power. Examples of Armstrong Numbers in JavaScriptHere are some examples of Armstrong numbers implemented in JavaScript: Example 1: Using a while loop Code In this section, we generalized the formula for finding an Armstrong number. First, we find the number of digits of the number input by the user and use that to decide the power required for calculation. We use a while loop to read each digit of the number one by one and then carry out the required processing. Output For a valid Armstrong number: ![]() For an Invalid Armstrong number: ![]() Example 2: Using for loopWe can also iterate using a for loop to find the sum. The strategy for finding the Armstrong number will remain the same in both cases. This is just for demonstration purposes of another JavaScript implementation. Code We have employed a for loop to find the sum in this example. We can keep going through each of the digits until we are done with all of them. Output For a valid Armstrong number: ![]() For an invalid Armstrong number: ![]() Conclusion
Next TopicJavaScript XOR |
The regex "\0" shows the available null character in the input string in JavaScript. If the null value is available, then the position of the values shows; otherwise, the "-1" value shows using the search method. We can test, search, and match the required string...
8 min read
Logical AND (&&) Operator in JavaScript Logical operators are a crucial part of JavaScript, a strong and adaptable computer language used for web development. One of the basic logical operators in JavaScript for carrying out logical operations is the logical AND (&&) operator. If all of the...
6 min read
We can create a rectangle with the help of JavaScript Canvas Rectangle. As we know rectangle is drawn using width and height and here in this article, we will draw a rectangle using those parameters. The three most common methods of drawing rectangles on canvas are: The rect()...
3 min read
How to activate JavaScript on Mac? To activate JavaScript on a Mac, follow these guidelines: JavaScript is a widely used programming language for creating dynamic, interactive web pages, such as applications and browsers. The steps for allowing JavaScript differ between various browsers on a Mac. If the changes do...
3 min read
In programming, camel case is a common writing style where the first word begins with a lowercase letter, and each subsequent word begins with an uppercase letter. This naming convention is particularly used in JavaScript for variables, functions, and methods. Variables like getUserInfo, fetchDataFromAPI, and userName...
8 min read
Sorting an array of objects in JavaScript is a fundamental operation that programmers often encounter in web development and general software engineering tasks. Understanding how to sort objects based on specific keys is crucial for efficiently organizing and manipulating data in JavaScript applications. In this discussion,...
7 min read
In JavaScript, we can hide the elements using the style.display or by using the style.visibility. The visibility property in JavaScript is also used to hide an element. The difference between the style.display and style.visibility is when using visibility: hidden, the tag is not visible, but...
2 min read
Reusable code can help developers create applications. Reusing code does not mean using it without the JavaScript function. Code segments are run at runtime by functions. The normal and Arrow functions are the two main uses of JS. In this article, we will discuss the difference between...
10 min read
JavaScript is one of the most generally utilized programming languages. It offers different operators for arithmetic operations, including subtraction. Understanding how the minus operator functions in JavaScript is essential for developers to control numerical values. In this article, we will explore the subtleties of the minus...
6 min read
Introduction JavaScript is a widely used and powerful computer language that allows developers to build dynamic and interactive websites. Many cases have the necessity of sorting numbers, and the Array.prototype.sort() method in JavaScript is a powerful and flexible method for this purpose. This post will describe...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India