In previous post How to reverse a number in Java we learnt how to reverse a positive or negative numbers in java

In this post we will write a java program to check Whether number is armstrong or not.

**What is Armstrong number?**

An **n-digit number** that is the sum of the th powers of its digits is called an Armstrong number.

In another words we can define Armstrong number as below:

We can define an ** Armstrong number** is a *number that is equal to the sum of the digits in a number raised to the power of number of digits in the number*.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * Java program to check Armstrong number */ public class ClientTest { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Take a number from keyboard using Scanner System.out.println("Enter Number:"); int number = scanner.nextInt(); boolean isArmstrong = isArmstrongNumber(number); if (isArmstrong) { System.out.println(number + " is an Armstrong number"); } else { System.out.println(number + " is not an Armstrong number"); } } catch (Exception e) { System.out.println("Please Enter valid number.."); e.printStackTrace(); } } private static boolean isArmstrongNumber(int number) { //convert number to String String numberToString = String.valueOf(number); int lengthOfNumber = numberToString.length(); int numberCopy = number; int sum = 0; while (numberCopy != 0) { int remainder = numberCopy % 10; //Here we use Math class pow function to get digit raised to the power total number of digits sum = sum + (int) Math.pow(remainder, lengthOfNumber); numberCopy = numberCopy / 10; } return (sum == number) ? true : false; } } |

**I have tested above program for few inputs as below. You may check for other input as well** .

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Enter Number: 153 153 is an Armstrong number Enter Number: 453 453 is not an Armstrong number Enter Number: 371 371 is an Armstrong number Enter Number: 9474 9474 is an Armstrong number Enter Number: 1 1 is an Armstrong number |

That’s all about this topic How to check Armstrong number java program

**You May Also Like:**

How to swap two numbers with or without temporary variable in java

Java Program to Swap two numbers using Bitwise XOR Operator?

How to reverse a number in Java

Java program to find factorial of a number

Java Program to Calculate the Power of a Number

Check whether a number is prime or not

Java Program to displaying prime numbers

Fibonacci series using iterative and recursive approach java program

Find largest and second largest number in the given Array

Java program to Remove Duplicate Elements From an Array

Find common elements between two Arrays

Find largest and smallest number in the given Array

Java Program to find duplicate elements in an Array

Count number of words in a string in java

How would you check if a number is even or odd using bit wise operator in Java?

How can you check if the given number is power of 2?

How to check if String is number in java ?

**If you have any feedback or suggestion please feel free to drop in blow comment box.**