Check if the given number is Armstrong Number in Java

Carvia Tech | May 18, 2019 | 2 min read | 19 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. How will you check if a given sentence is a pangram
  2. Calculate factorial of a number in Java using recursion
  3. Write a program to reverse a string using recursion in Java
  4. Check a number is Prime: Java Coding Problem
  5. Create anagram buckets from a given input array of words
  6. Find two numbers of which the product is maximum in an array
  7. Java program to check if two strings are anagrams
See all articles in Java Coding Challenges
Top articles in this category:
  1. Top 50 SDET Java Programming Interview Questions & Answers
  2. Check whether given number is even or odd
  3. How to check if the given number is palindrome in Java
  4. Check a number is Prime: Java Coding Problem
  5. How to reverse a number in Java
  6. Calculate factorial of a number in Java using recursion
  7. Calculate Fibonacci Series in Java



Find more on this topic:
SDET Interviews image
SDET Interviews

End to end automation testing using Selenium Web Driver, Rest Assured, JMeter, Junit, TestNG etc.

Last updated 1 month ago


Recommended books for interview preparation:

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