Armstrong Number
Write a program to print armstrong number
public class Armstrong {
public static void main(String[] args) {
int i=1;
for(;i<10000;i++) {
boolean arms=isArmStrong(i);
if(arms)
System.out.println(i);
}
}
static boolean isArmStrong(int n) {
int sum=0,t=n;
int pow=countDigit(n);
do {
int rem=n%10;
sum=sum+Pow(rem, pow);
n=n/10;
}while(n!=0);
return sum==t;
}
static int countDigit(int n) {
int count=0;
do {
count++;
n/=10;
} while (n!=0);
return count;
}
static int Pow(int n,int p) {
int pow=1;
while(p>0) {
pow=pow*n;
p--;
}
return pow;
}
}
Comments
Post a Comment