Techno Blender
Digitally Yours.

JavaScript Program for Armstrong Numbers

0 35


In this article, we will see JavaScript program to check 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 digit. 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.

There are several methods that can be used to check if a number is an Armstrong number or not, which are listed below:

We will explore all the above methods along with their basic implementation with the help of examples.

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);

Output
9474 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;

  

    

    while (temp) {

        remainder = temp % 10;

  

        

        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 order(number) {

    let n = 0;

    while (number > 0) {

        n++;

        number = Math.floor(number / 10);

    }

    return n;

}

  

isArmstrong(153);

  

isArmstrong(520);

Output
153 is an Armstrong Number
520 is Not an Armstrong Number

Approach 3: Using Array.from() Method 

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;

  

    

    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(1634);

  

isArmstrong(749);

Output
1634 is an Armstrong Number
749 is not an Armstrong Number

Last Updated :
29 Jun, 2023

Like Article

Save Article


In this article, we will see JavaScript program to check 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 digit. 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.

There are several methods that can be used to check if a number is an Armstrong number or not, which are listed below:

We will explore all the above methods along with their basic implementation with the help of examples.

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);

Output
9474 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;

  

    

    while (temp) {

        remainder = temp % 10;

  

        

        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 order(number) {

    let n = 0;

    while (number > 0) {

        n++;

        number = Math.floor(number / 10);

    }

    return n;

}

  

isArmstrong(153);

  

isArmstrong(520);

Output
153 is an Armstrong Number
520 is Not an Armstrong Number

Approach 3: Using Array.from() Method 

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;

  

    

    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(1634);

  

isArmstrong(749);

Output
1634 is an Armstrong Number
749 is not an Armstrong Number

Last Updated :
29 Jun, 2023

Like Article

Save Article

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment