Java Program for Armstrong Number

Example Java Program Source code to check ArmStrong Number


Armstrong number is a number where the sum of their own digits to the power of the number of digits will be equal to the original number. Some of the armstrong number are shown below
1^3 + 5^3 + 3^3 = 153
3^3 + 7^3 + 1^3 = 371
1^4 + 6^4 + 3^4 + 4^4 = 1634

The below java code will check the given number whether it is armstrong number or not.
//ArmstrongNumber.java

import java.io .*;
class ArmstrongNumber{
public static void main(String[] args){
int n, d=0, s=0;
try{
n=Integer.parseInt(args[0]);
int t=n;
while(n!=0){
d=n%10;
s=s+d*d*d;
n=n/10;
}
if(s==t)
System.out.println(args[0]+ "is Armstrong Number" );
else
System.out.println(args[0]+ "is Not a Armstrong Number");
}catch(Exception e){}
}
}


To Run the above program
>javac ArmstrongNumber.java
>java ArmstrongNumber 153
>153 is ArmStrong Number


Related Topic Java Program to Check Armstrong Number
Java - Create Random Number
Servlet Interview Questions & Answers
JAVA Number-Format Example
C Program for Armstrong Number
Java  

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback