Check if Given number is Armstrong Number or not in Java [Java interview]

Check if Given number is Armstrong Number or not in Java

This article will help you to understand “Java Program Of Armstrong Number.”

Armstrong numbers (also known as narcissistic number) are the sum of their own digits to the power of the number of digits. e.g. for 371. Number of digits = 3, So 33 + 73 + 13 = 27 + 343 + 1 = 371. So this is Armstrong number.

Java Program Of Armstrong Number

package com.codenuclear;

public class ArmstrongNumber {

	public boolean checkArmstrongNumber(int inputNumber) {

		int temp = inputNumber;

		int digits = String.valueOf(inputNumber).length();
		int sum = 0;
		int div = 0;
		while (temp > 0) 
		{
			div = temp % 10;
			int tempTotal = 1;
			for (int i = 0; i < digits; i++) {
				tempTotal *= div;
			}
			sum += tempTotal;
			temp = temp / 10;
		}

		if (inputNumber == sum) 
		{
			return true;
		} 
		else {
			return false;
		}
	}

	public static void main(String args[]) 
	{
		ArmstrongNumber obj = new ArmstrongNumber();
		System.out.println("Is 407 Armstrong number? :- " + obj.checkArmstrongNumber(407));
		System.out.println("Is 515 Armstrong number? :- " + obj.checkArmstrongNumber(515));
		System.out.println("Is 371 Armstrong number? :- " + obj.checkArmstrongNumber(371));
	}
}

Output Is 407 Armstrong number? :- true Is 515 Armstrong number? :- false Is 371 Armstrong number? :- true