### 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

**. To start with, I am going to discuss a simple problem of finding if a number is an ‘Armstrong’.**

*‘Coding Exercise Interview’*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 n ^{th} powers of its digits.*

**For example:**

153 is an Armstrong number because 153 = 1

^{3}+ 5

^{3}+ 3

^{3 }

370 is an Armstrong number because 370 = 3

^{3}+ 7

^{3}+ 0

^{3}

1634 is an Armstrong number because 1

^{4}+ 6

^{4}+ 3

^{4}+ 4

^{4}= 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: **

/** * A simple Java program to find out if the entered number is an Armstrong Number or not. * Armstrong numbers are the sum of their own digits to the power of the number of digits. * For Example- 2 Armstrong numbers shown below: * A) 370 = 33 + 73 + 03 = 27 + 343 + 0 = 370 * B) 9474 = 94 + 44 + 74 + 44 = 6561 + 256 + 2401 + 256 = 9,474 */ import java.util.Scanner; /** * @author Shalini Goyal * */ public class ArmstrongNumbers { public static void main(String[] args) { int number = new Scanner(System.in).nextInt(); //Validate the number before sending for Armstrong check number = validateTheNumber(number); //Check if Armstrong or not if(isArmstrong(number)){ System.out.println( number + " is an Armstrong Number. "); }else{ System.out.println( number + " is not an Armstrong Number. "); } } /** * Method to check if the number is a valid one * @param number * @return int */ static int validateTheNumber(int number){ int digitCount = String.valueOf(number).length(); while(digitCount < 3 ){ System.out.println( number + " is not a 3 digit number. Please enter a 3 digit or more number: "); number = new Scanner(System.in).nextInt(); digitCount = String.valueOf(number).length(); } return number; } /** * Method to calculate number's authenticity of being Armstrong * @param enteredNumber * @return boolean */ public static boolean isArmstrong(int number){ int digitCount = String.valueOf(number).length(); int sumOfExpoDigits = 0; int originalNumber = number; //Calculate the sum of digits' to the power of the number of digits while(number != 0){ int remainder = number % 10; sumOfExpoDigits = sumOfExpoDigits + (int)Math.pow(remainder, digitCount); number = number /10; } //Compare and find out the result if(sumOfExpoDigits == originalNumber){ return true; }else{ return false; } }//End of method }

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 !!!