write a java program to pass a three digit number and check it is an Armstrong number or not . [ sum of the cube of digits = number]
Answers
Explanation:
Check Armstrong Number for 3 digit number Then, a while loop is used to loop through originalNumber until it is equal to 0. On each iteration, the last digit of num is stored in remainder . Then, remainder is powered by 3 (number of digits) using Math. pow() function and added to result .
Explanation:
A positive integer is called an Armstrong number of order n if
abcd... = an + bn + cn + dn + ...
In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example:
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
Example 1: Check Armstrong Number for 3 digit number
public class Armstrong {
public static void main(String[] args) {
int number = 371, originalNumber, remainder, result = 0;
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}
if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number