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 e.g. calculating factorial, reversing string or calculating prime numbers, this is a good exercise to build programming logic. It teaches you basic programming technique of how to use 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 number palindrome exercise. If you modulo an integer number by 10, you will get last digit, for example 656%10 will give you 6, which is last digit of 656. Similarly to reduce the input after each iteration, you can use division operator, as 656/10 will return 65, which is without last digit. If you know this trick, it's very easy to solve any programming problem, which requires examination of individual digits. This Java program uses same technique and compute 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 number. Well, for that you need to remember 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 sum of power four of individual digits of that number. Another interesting challenge is to write a program which uses recursion to find if number is Armstrong or not.
No comments:
Post a Comment