Check if the given number is Armstrong Number in Java

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



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