Armstrong Number in Java

Carvia Tech | July 24, 2020 | 2 min read | 701 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.
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 {

    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. Write a program to reverse a string using recursion in Java
  2. How to reverse a number in Java
  3. Palindrome checker in Java
  4. Find two numbers of which the product is maximum in an array
  5. Check if the given string is palindrome
  6. Anagrams string checker in Java
  7. Create anagram buckets from a given input array of words
See all articles in Java Coding Challenges
Top articles in this category:
  1. SDET Java Coding Challenges
  2. 50 SDET Java Interview Questions & Answers
  3. Rest Assured API Testing Interview Questions
  4. SDET: JUnit interview questions for automation engineer
  5. Check a number is Prime: Java Coding Problem
  6. How to reverse a number in Java
  7. Factorial in Java using recursion

Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info