Java Program to Check Whether a Given Number is Armstrong or not

Write a Write a Java program to check whether a given number is armstrong or not.



Source Code
class ArmStrongNum {

	public static void main(String[] args) {

		int num = Integer.parseInt(args[0]);
		int n=num,res=0;

		while(n>0) {
			
			res = res + (int)Math.pow(n%10,3);
			n = n /10;
		}
		
		if(num == res) {
			
			System.out.println(num+" == "+res);
			System.out.println("Hence the "+num+" is an armstrong no.");
		}
		else {
			
			System.out.println(num+" != "+res);
			System.out.println("Hence the "+num+" is not an armstrong no.");
		}
	}
}
Output
C:\>javac ArmStrongNum.java
C:\>java ArmStrongNum 371
371 == 371
Hence the 371 is an armstrong no.

C:\>javac ArmStrongNum.java
C:\>java ArmStrongNum 515
515 != 251
Hence the 515 is not an armstrong no.