Armstrong number is a 3 digit number which is equal to sum of cube of its digits.
For example: 371,153
For example: 371,153
In this post, we will see how to check for Armstrong number in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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; } } |
1 2 3 4 5 |
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.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.