Java program to check Armstrong number

Armstrong number is a 3 digit number which is equal to sum of cube of its digits.
For example: 371,153
In this post,  we will see how to check for Armstrong number in java.
class CheckArmStrongNumberMain
{
 public static void main(String[] args)
 {
   CheckArmStrongNumberMain casnm=new CheckArmStrongNumberMain();
  System.out.println(" Is 153 Armstrong number: "+casnm.isArmStrongNumber(153));
  System.out.println(" Is 234 Armstrong number: "+casnm.isArmStrongNumber(234));
     System.out.println(" Is 371 Armstrong number: "+casnm.isArmStrongNumber(371));
 }
 public boolean isArmStrongNumber(int number)
 {
  int sum=0;
  int originalNumber=number;
  while(number!=0)
  {
   
  int remainder=number%10;
  sum=sum+remainder*remainder*remainder;
  number=number/10;
  }
  if(originalNumber==sum)
  {
   return true;
  }
  return false;
 }
}
When you run above program, you will get following output.
 Is 153 Armstrong number: true
 Is 234 Armstrong number: false
 Is 371 Armstrong number: true

Please go through Frequently asked java interview Programs for more such programs.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012