Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » Java » Programs » Armstrong Number

Java program on Armstrong Number

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407



public class Main {

	public static void main(String[] args) {
	    System.out.println("## Java program on Armstrong number ##");
		// Armstrong number --> 153 ----> 1*1*1 + 5*5*5 + 3*3*3 = 153
		
		int num = 153;
		int actualNum = num;
		double result = 0;
		
		while(actualNum != 0) {
			int n = actualNum % 10;
			result = result + Math.pow(n, 3);
			actualNum = actualNum / 10;
		}		
		if(result == num) {
			System.out.println(num + " is an armstrong number");
		}
		else {
			System.out.println(num + " is not an armstrong number");
		}
	}
}