Pages

How to check given number is Armstrong?

 Java Armstrong program ?

>> In Java we can verify given number, string is armstrong or not.

>> Palindrome is a number, it should be same after the sum of cube of individual digits.

Ex:
153=(1*1*1)+(5*5*5)+(3*3*3)
371=(3*3*3)+(7*7*7)+(1*1*1)

public class armstrong {
public static void main(String[] args) {
int num = 153, temp = num;
int rem, sum = 0;
while (num > 0) {
rem = num % 10;
sum = sum + (rem * rem * rem);
num = num / 10;
}

if (temp == sum) {
System.out.
print("Given number " + temp + " is Armstrong");
}
else {
System.out.
print("Given number " + temp + " is not Armstrong");
}
}
}
Output:
Java Armstrong
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.