Find out if a given Number is an Armstrong Number or not !!!
It is quite common to get asked about a coding question these days. Interviewers have started preferring looking into the coding or programming skills in addition to asking the technical questions.
Even a simple coding exercise can brought out many hidden aspects of person like practical knowledge, confidence, way of thinking, logical and analytic ability etc.
I am going to share few such coding exercises which are quite common and are always good to practice if you are going to appear for a ‘Pair Programming Interview’ or a ‘Coding Exercise Interview’. To start with, I am going to discuss a simple problem of finding if a number is an ‘Armstrong’.
Quite often people are not aware about Armstrong numbers but if not mentioned already, one should never hesitate asking about it. After all, the coding exercise is about checking the coding skills and not the knowledge of ‘Armstrong Numbers’ 🙂 Anyways, here is a simple description of Armstrong Numbers:
An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.
153 is an Armstrong number because 153 = 13+ 53+ 33
370 is an Armstrong number because 370 = 33 + 73+ 03
1634 is an Armstrong number because 14 + 64 + 34 + 44 = 1634.
In case you want to learn more, please refer the link: http://en.wikipedia.org/wiki/Narcissistic_number
There can be multiple coding approaches to this problem. You can find the sample code with one approach below.
Here is the code:
Executing the above code by giving any 3 digit number and it would simply print if the the number is Armstrong or not. There can be different approaches for implementing this. Try and share !!!