An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number, since 1**3 + 5**3 + 3**3 = 153, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. Along with usual beginner exercises like calculating factorial, reversing a string, or calculating prime numbers, this is a good exercise to build programming logic. It teaches you basic programming techniques of how to use the operator for something which is not obvious.
For example, to solve this programming challenge, we first need to check if a number is Armstrong or not, and to do this we need individual digits of the number. how do we do that? well, there is a programming technique, which you might have learned while doing the number palindrome exercise.
If you modulo an integer number by 10, you will get the last digit, for example, 656%10 will give you 6, which is the last digit of 656. Similarly to reduce the input after each iteration, you can use the division operator, as 656/10 will return 65, which is without the last digit.
If you know this trick, it's very easy to solve any programming problem, which requires the examination of individual digits. This Java program uses the same technique and computes all Armstrong numbers in the range of 0 and 999.
By the way, this program has different variations as well e.g. how do you find Armstrong number of four-digit, as this program only calculates three-digit Armstrong numbers.
If you modulo an integer number by 10, you will get the last digit, for example, 656%10 will give you 6, which is the last digit of 656. Similarly to reduce the input after each iteration, you can use the division operator, as 656/10 will return 65, which is without the last digit.
If you know this trick, it's very easy to solve any programming problem, which requires the examination of individual digits. This Java program uses the same technique and computes all Armstrong numbers in the range of 0 and 999.
By the way, this program has different variations as well e.g. how do you find Armstrong number of four-digit, as this program only calculates three-digit Armstrong numbers.
Well, for that you need to remember the general definition of Armstrong number which is, An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.
So a four-digit Armstrong number will be equal to the sum of power four of individual digits of that number. Another interesting challenge is to write a program that uses recursion to find if the number is Armstrong or not.
So a four-digit Armstrong number will be equal to the sum of power four of individual digits of that number. Another interesting challenge is to write a program that uses recursion to find if the number is Armstrong or not.
Armstrong Number Code Example in Java
Here is our Java program to display all Armstrong numbers between 0 and 9999. Actually, there are only three digits Armstrong numbers in that range. Our solution is simple but general, we have a loop that runs up to a number entered by the user.So if the user wants to see Armstrong's number between 0 and 9999, he should enter 9999. Though it has one shortcoming, the logic of checking if the number is Armstrong or not is hard-coded to find only three-digit numbers.
So, precisely this is a program to display the digit Armstrong number between 0 to 9999 or any user-supplied upper range. Coming back to logic, all it does is :
- Extract individual digits of the number in each iteration
- Calculate cube of that digit and add into a sum which is initialized with zero
- reduce the number by a factor of 10 to remove one digit.
This logic is encapsulated inside a private static method called isArmstrongNumber(int number). This is again called in a loop to supply all the numbers from 0 to 9999. Logic is simple but presents a powerful technique to solve any problem which is based on individual digits of numbers.
Java Program to Print Armstrong Number
import java.util.Arrays; import java.util.Scanner; /** * This Java program computes all Armstrong numbers in the range of 0 and 9999. An * Armstrong number is a number such that the sum of its digits raised to the * third power is equal to the number itself. For example, 153 is an Armstrong * number, since 1**3 + 5**3 + 3**3 = 153. * * @author Javin Paul */ public class ArmstrongNumberDemo{ public static void main(String args[]) { Scanner cmd = new Scanner(System.in); System.out.println("Please enter a number up-to which Armstrong number will be find"); int count = cmd.nextInt(); int index = 0; for (int i = 0; i < count; i++) { if (isArmstrongNumber(i)) { System.out.printf("Armstrong number %d: %d %n", index, i); index++; } } cmd.close(); } /** * Java Method to check if given number is Armstrong Number or not * * @param number * @return true, if Armstrong number, false otherwise. */ public static boolean isArmstrongNumber(int number) { int sum = 0; int copyOfInput = number; while (copyOfInput != 0) { int lastDigit = copyOfInput % 10; sum += (lastDigit * lastDigit * lastDigit); copyOfInput /= 10; } if (sum == number) { return true; } return false; } } Output Please enter a number up-to which Armstrong number will be find 9999 Armstrong number 0: 0 Armstrong number 1: 1 Armstrong number 2: 153 Armstrong number 3: 370 Armstrong number 4: 371 Armstrong number 5: 407
That's all about how to find Armstrong numbers in Java. As I said this program is a very popular coding exercise for Java beginners and there are a lot of versions exist e.g. writing a program to check if the given number is Armstrong or not, that could be any digit long so you need to write logic which can check it correctly.
Similarly, there is one more version that exists, writer program to print Armstrong number of four or five digits. The core logic of checking if a number is Armstrong or not is the same, but you need to tweak them a little bit to solve these programming problems.
Apart from this, I would recommend the following programs to any Java beginners :
- 1.Write a Java Program to See if two String are Anagram of each other? (Solution)
- 2. How to count occurrences of a character in String? (Solution)
- 3. How to find first non-repeated characters from String in Java? (See here for solution)
- 4. Write a Program to check if a number is binary in Java? (Solution)
- 5. How to remove duplicates from array without using Collection API? (Solution)
- 6. Write a Program to calculate Sum of Digits of a number in Java? (Solution)
- 7. Write a Program to prevent Deadlock in Java? (Click here for solution)
- 8. Write a Program to solve Producer-Consumer problems in Java. (Solution)
- 9. How to reverse String in Java without using API methods? (Solution)
- 10. How to check if Array contains duplicate numbers or not? (Solution)
- 11. How to remove duplicates from ArrayList in Java? (Solution)
Thanks for reading this article so far, if you like this article and my solution of finding and printing Armstrong number in Java then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are preparing for coding interviews and need resources to further hone your coding skills before interviews then you can also checkout this list of 10 best coding interview courses to quickly revise essential concepts and skills before your next coding interview.
And lastly one question for you, which one is your favorite Java coding exercise? Prime number, Palindrome, String permutation, reverse a linked list or this one? Do let me know in comments and suggest topic if you want me to write on that.
1 comment :
Hello,this program doesn't work correct.Because here (lastDigit*lastDigit*lastDigit) is static it is not dynamic,so you can't get the result as(1634,8208,9474);
Post a Comment