In this post we’ll see a Java program to check if the passed number is an **Armstrong number** or not. A number is an Armstrong number if it is equal to the number you get by raising every digit of the number to the power of count of digits in the number and adding them.

**Armstrong number example**–

371 = 3^{3} + 7^{3} + 1^{3} = 27 + 343 +1 = 371

Number of digits is 3 here so every digit is raised to the power of 3 and added. Since the calculated number is equal to the original number so 371 is an Armstrong number.

1634 = 1^{4} + 6 ^{4} + 3^{4} + 4^{4} = 1 + 1296 + 81 + 256 = 1634

#### Java code to check if number Armstrong number or not

import java.util.Scanner; public class ArmstrongNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a number : "); int number = sc.nextInt(); System.out.println("Is " + number + " an Armstrong number- " + checkIfArmstrong(number)); sc.close(); } private static boolean checkIfArmstrong(int number){ // Converting to string and calculating length int numLength = (number+"").length(); int temp = number; int sum = 0; while(temp != 0 ){ int remainder = temp % 10; sum = sum + (int)Math.pow(remainder, numLength); temp = temp/10; } if(number == sum){ return true; }else{ return false; } } }

__Output__

Please enter a number : 371 Is 371 an Armstrong number- true Please enter a number : 1634 Is 1634 an Armstrong number- true Please enter a number : 373 Is 373 an Armstrong number- false

**Related Posts**

- Java Program to Display Armstrong Numbers
- Display Prime Numbers in Java
- Arrange Given Numbers to Form The Biggest Number in Java
- Java Program to Swap Two Numbers Without Using Third Variable
- Java Program to Check if The Given Strings Are Anagram or Not
- How to Split a String in Java
- How to List All The Files in a Directory in Java
- Read PDF in Java Using iText

That’s all for the topic **Java Program to Check if Armstrong Number**. If something is missing or you have something to share about the topic please write a comment.

**You may also like**