Exercise:
Write a Java program to check Armstrong number
1.Check for 3 digit numbers
Click Here to View the Solution!
public
class ArmstrongFinder {
public static void main(String[] args) {
int number =
489, 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 +
" is not an Armstrong number.");
}
}
Click Here to View the Output!
489 is not an Armstrong number.
Click Here to View the Explanation!
- This program is used for checking that whether a 3-digit number is an Armstrong number or not by using a
for loop
. - Three digit integer variables are initiated as
number = 489
,originalNumber
, remainder and result = 0. - Initially, the value of number is stored into the variable
originalNumber
. This is done because the original number and the result are compared at the end. - A
while loop
is set which will hold the condition that the loop will continue until the value oforiginalNumber
becomes zero (originalNumber != 0
). - In the body of the
while loop
, theoriginalNumber
is divided by 10 and its remainder is stored in the variable remainder which means removing the last digit of the number. - Then the value of the remainder will be powered by 3, which is the number of the digits of the
originalNumber
, by using a Java math function (Math.pow()
) and added into the variable result. - In the end of the loop, the number is divided by 10 in order to remove the last digit of
originalNumber
. - After the loop exits, both the result and the number are compared. If both are equal, then the following message is printed: “’
number
’ is an Armstrong number.” Else it will print “’number
’ is not an Armstrong number”.
2.Check for n-digits
Click Here to View the Solution!
public
class ArmstrongFinder {
public static void main(String[] args) {
int number =
9474, originalNumber, remainder, result =
0, n =
0;
originalNumber = number;
for (;originalNumber !=
0; originalNumber /=
10, ++n);
originalNumber = number;
for (;originalNumber !=
0; originalNumber /=
10)
{
remainder = originalNumber %
10;
result += Math.pow(remainder, n);
}
if(result == number)
System.out.println(number +
" is an Armstrong number.");
else
System.out.println(number +
" is not an Armstrong number.");
}
}
Click Here to View the Output!
9474 is an Armstrong number.
Click Here to View the Explanation!
- This program is used to find out that whether a number having n-digits is an Armstrong number or not.
- Five integer variables are initiated as
number = 9474
,originalNumber
, remainder,result = 0
andn = 0
. - Initially, the value of number is stored into the variable
originalNumber
. This is done because the original number and the result are compared at the end. - This program uses two
for loops
instead of a singlewhile loop
used in the previous program. - The first
for loop
is used to find out the number of digits that make up the number. And then store the value in the variablen
. - Whereas, the second
for loop
is used to calculate the remainder of the number and the result by powering the remainder with the number of digits n in every iteration. - The last digit of the number is removed within the for loop and in the end, both the
result
and thenumber
are compared. If both are equal, then the following message is printed:“’number’ is an Armstrong number.”
. Else it will print“’number’ is not an Armstrong number”
.