In this article, we will define an Armstrong Number in Java. We’ll look at how to determine whether a number is Armstrong or not, as well as its code. In Java coding interviews and academics, the Armstrong number programme is frequently asked.
Armstrong Number
In java, a number is considered an Armstrong number if the sum of its own digits raised to the power number of digits gives the number itself. A positive m-digit number that is the sum of its digits’ mth powers is known as an Armstrong number.
Examples of Amstrong numbers include 0, 1, 153, 370, 371, 407, 1634, 8208, and 9474.
153 = 13 + 53 + 33 = 1 + 125+ 27 = 153
If a positive integer is an Armstrong number of order n, then
abcd... = an + bn + cn + dn + ...
Java Program
The java example below shows how to check the given number is a Amstrong number.
package in.yawin; public class Armstrong { public static void main(String[] args) { int value = 153; int temp = value; int armstrongValue = 0; int remainder = 0; while (temp != 0) { remainder = temp % 10; armstrongValue = armstrongValue + (remainder * remainder * remainder); temp = temp / 10; } if (value == armstrongValue) { System.out.println(value + " is a Armstrong number."); } else { System.out.println(value + " is not a Armstrong number."); } } }
Output
153 is a Armstrong number.
Explanation
The java programme demonstrates how to determine whether a given number is amstrong number or not. The digits of the given number will be found by the Java programme. It divides the number by 10 to find the remaining digits after finding the last one using mod 10. The programme calculates the number’s cubes and adds a local variable after locating the last digit. The programme keeps running until it locates the final digit. The local variable holds the total of all the digits of the given number’s cubes. The java programme will determine whether the given value is amstrong number or not.