Monday, April 3, 2017

To check the given number is Amstrong or Not

package date24;

public class Amstrong {

public static void main(String[] args) {
int first = 153;

int num = first;
int digits = 0;
while (num > 0) {
num = num / 10;
digits++;
}
int s = 0;
int n = first;
while (n > 0) {
int rem = n % 10;
int sum = (int) Math.pow(rem, digits);
n = n / 10;
s = s + sum;
}
if (first == s) {
System.out.println(" the given number is amstrong");
} else {
System.out.println("not amstrong");
}
}
}




OutPut:
 the given number is amstrong






0 comments:

Post a Comment