Armstrong Number in Java

Upasana | July 24, 2020 | 2 min read | 794 views | Java Coding Challenges


Armstrong number

In an Armstrong number (also known as narcissistic number), is a number that is the sum of its own digits each raised to the power of the number of digits.

xy...z = x^n + y^n + ... + z^n

where n is the number of digits in a number.

Few examples are: 153, 371, 407, 8208, etc.

153 = 13 + 53 + 33
8208 = 84 + 24 + 04 + 84

Armstrong number: Java implementation

We can write a simple Java program to check if the given input is armstrong number or not.

Here is the approach:

  1. Count the number of digits in a number

  2. extract digits from input number one by one using remainder operation

  3. calculate the sum of powers of each digit

  4. if the sum of powers is same as the input itself, then number is armstrong else not.

Armstrong.java
public class Armstrong {

    public boolean check(int input) {
        int temp, digit, sumOfPower = 0;
        temp = input;
        int digits = countDigit(input);
        while (temp != 0) {
            digit = temp % 10;
            System.out.println("Current Digit is " + digit);
            sumOfPower = sumOfPower + (int) Math.pow(digit, digits);
            System.out.println("Current sumOfPower is " + sumOfPower);
            temp /= 10;
        }
        return sumOfPower == input;
    }

    static int countDigit(long n) {
        return (int) Math.floor(Math.log10(n) + 1);
    }
}

We know that only 4 armstrong numbers that are of 3 digits (i.e. from 100 to 999), we can write a simple JUnit test to assert the same.

JUnit testcase
import org.junit.Test;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;

public class ArmstrongTest {

    @Test
    public void check() {
        Armstrong checker = new Armstrong();
        assertThat(checker.check(153), equalTo(true));
        assertThat(checker.check(370), equalTo(true));
        assertThat(checker.check(371), equalTo(true));
        assertThat(checker.check(407), equalTo(true));
        assertThat(checker.check(8208), equalTo(true));
    }
}

That’s all.


Java Coding Challenges:
  1. Reverse position of words in a string using recursion
  2. Check if the given string is palindrome
  3. Find two numbers of which the product is maximum in an array
  4. Prime number checker in Java
  5. Create anagram buckets from a given input array of words
  6. Anagrams string checker in Java
  7. Reverse a string using recursion in Java
See all articles in Java Coding Challenges
Top articles in this category:
  1. Java Coding Problems for SDET Automation Engineer
  2. 50 Java Interview Questions for SDET Automation Engineer
  3. Prime number checker in Java
  4. Java Program to find Factorial of a number
  5. How to reverse a number in Java
  6. Rest Assured API Testing Interview Questions
  7. Find two numbers of which the product is maximum in an array

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.