A positive number is called an Armstrong number if it is equal to the cubic sum of that number. For example, 0, 1, 153, 370, 371, 407.
For example:
Lets take 153
153 = (111)+(555)+(333)
(111)=1
(555)=125
(333)=27
So:
1+125+27=153
Program to check number is Armstrong Number or not
public class ArmstrongNumber { public static void main(String[] args) { int num = 407; int rem = 0; int tempNumber = 0; int sum = 0; tempNumber = num; while (tempNumber != 0) { rem = tempNumber % 10; sum = sum + rem * rem * rem; tempNumber = tempNumber / 10; } if (sum == num) { System.out.println(num + " is an Armstrong number"); } else { System.out.println(num + " is not an Armstrong number"); } } }
Output
407 is an Armstrong number
Program to check number is Armstrong Number or not using for loop
public class ArmstrongNumber { public static void main(String[] args) { int num = 153; int rem = 0; int tempNumber = 0; int sum = 0; tempNumber = num; for (; tempNumber != 0; tempNumber /= 10) { rem = tempNumber % 10; sum = sum + rem * rem * rem; } if (sum == num) { System.out.println(num + " is an Armstrong number"); } else { System.out.println(num + " is not an Armstrong number"); } } }
Output
153 is an Armstrong number
Program to Check Armstrong number for n digits
An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.
Example:
1634= 1^4 + 6^4 + 3^4 + 4^4 = 1+1296+81+256 = 1634
public class ArmstrongNumber { public static void main(String[] args) { int number = 1634; int tempNumber = 0; int rem = 0; int value = 0; int n = 0; tempNumber = number; for (; tempNumber != 0; tempNumber /= 10, n++); tempNumber = number; for (; tempNumber != 0; tempNumber /= 10) { rem = tempNumber % 10; value += Math.pow(rem, n); } if (value == number) { System.out.println(number + " is an Armstrong number."); } else { System.out.println(number + " is not an Armstrong number."); } } }
Output
1634 is an Armstrong number.