Java program to check for an Armstrong Number

This tutorial provides the java implementation for Armstrong Number determination. It first explains what exactly is an Armstrong number, then provides the java code to check whether a given number is an Armstrong number or not, and finally provides a detailed step-by-step explanation of the java code.

What is Armstrong Number
Any number which equals the sum of each of its digits raised to the power of the total number of digits is an Armstrong number.
Other names for Armstrong number – Narcissistic number, pluperfect digital invariant, plus perfect number.

To check a given number for an Armstrong number the following steps need to be followed

  1. Count the total number of digits in the given number.
    Lets say it is ‘n‘.
  2. Separate the digits of the number.
    Lets say the number is – ‘d1 d2…dn‘ where dk is the digit at position ‘k‘.
  3. Calculate the sum of each of the digits raised to the total count of digits.
    This will be – (d1)n+(d2)n…+(dn)n.
  4. If the given number equals the sum obtained in step-3, then its an Armstrong number, else not.

Example to show Armstrong number determination
Input number to check – 370
Applying the above given 4 steps –

  1. Number of digits in 370 – 3.
  2. Separate the digits in 370 – 3,7,0.
  3. Calculate the sum –(3)3+(7)3+(0)3 = 370.
  4. Sum obtained in step-3 is 370 which is equal to the given number. Hence, 370 is an Armstrong number.

Java code to determine whether given number is Armstrong or not

Java code to check for an Armstrong number
package com.javabrahman.generaljava;
import java.util.Scanner;
public class ArmstrongNumberCheck {
  public static void main(String args[]) {
    //Input number to be tested
    Scanner scanner = new Scanner(System.in);
    String str = scanner.nextLine();
    //Convert number in String format to Long format
    long inputNo = Long.parseLong(str);
    //Call to method to count number of digits
    int digitCount = countNoOfDigits(inputNo);
    //call to method to calculate sum
    long sumOfDigits = calculateSum(inputNo, digitCount);
    if (inputNo == sumOfDigits) {
      System.out.println(inputNo + " is an Armstrong Number");
    } else {
      System.out.println(inputNo + " is NOT an Armstrong Number");
    }
  }
  /**
   * Method counts the number of digits of inputNo
   * @param inputNo
   * @return count of digits
   */
  public static int countNoOfDigits(long inputNo) {
    int count = 1;
    while ((inputNo = inputNo / 10) > 0) {
      count++;
    }
    return count;
  }
  /**
   * Method calculates the sum of digits
   * with each raised to the power of count of digits
   * @param inputNo
   * @param digitCount
   * @return sum of digits raised to power count of digits
   */
  public static long calculateSum(long inputNo, int digitCount) {
    long sum = 0;
    long num = inputNo;
    while (num > 0) {
      long remainder = num % 10;
      sum += Math.pow(remainder, digitCount);
      num = num / 10;
    }
    return sum;
  }
}
 OUTPUT for different input values
370
370 is an Armstrong Number
400
400 is NOT an Armstrong Number
Explanation of the code

  • ArmstrongNumberCheck uses a Scanner instance to fetch the given number entered by the user to be checked for an Armstrong number.
  • The input number is converted from a String value to a long value, named inputNo, using Long.parseLong() method.
  • inputNo is sent as a parameter to the method countNoOfDigits() which returns the total number of digits in inputNo and stores it in the variable digitCount.
  • countNoOfDigits() works by dividing the inputNo by 10 in a loop. For every iteration the rightmost digit is removed (as remainders are not retained in a long value). So, for n iterations, n rightmost digits are removed, and once the number is 0, there is no digit remaining. At this point n will be the count of number of digits in the number.
  • inputNo and digitCount are sent as parameters to calculateSum() method.
  • calculateSum() method separates the digits of inputNo by using %(remainder) by 10. In each loop, the remainder on dividing by 10 is the rightmost digit of the number. Math.pow() method is used to calculate the sum of digits raised to the total count of digits.
  • The sum obtained is returned to the main() method. This sum value is compared to the inputNo to determine whether the given number is an Armstrong number or not.

Summary
In this tutorial we looked at what is an Armstrong number and then saw Java code for implementing Armstrong number check along with detailed explanation of the code.

 

Digiprove sealCopyright © 2014-2019 JavaBrahman.com, all rights reserved.