Java Program to Check Armstrong Number - The Coding Shala

Home >> Java Programs >> Armstrong Number

 In this post, we will learn how to check if a given number is Armstrong Number in Java.

Java Program to Check Armstrong Number

A positive number is called Armstrong Number if the following equation is true:

ab...z = a^n + b^n + .... + z^n

Here, n is the number of digits in the number.

For example, let's take 3 digits Armstrong Number.  

Example 1:
Input: 153
Output: Armstrong Number
Explanation: 
        153 = 1*1*1 + 5*5*5 + 3*3*3
              = 1 + 125 + 27
             = 153

Java Program: 

// Java program to Check Armstrong Number

public class Main {
	
	public static void main(String[] args) { 
		
		// input number to check Armstrong
		int number = 370; 
		
		int sum = 0, temp = number;
		while(temp != 0) {
			int digit = temp % 10;
			temp = temp / 10;
			sum += (digit * digit * digit);
		}
		
		if(sum == number) {
			System.out.println(number + " is a Armstrong number");
		} else {
			System.out.println(number + " is not a Armstrong number");
		}
	}
}

Output: 

370 is a Armstrong number


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

New Year Chaos Solution - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Method Overloading - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala