/* Lab : Lab4 Day : Tuesday [ 19 Aug 08 ] Problem Number: 02 Problem Title : Armstrong number Theory : The problem requires following 1. Finding of individual digits of a number 2. Finding summation of cubes of individual digits 3. Testing whether both the numbers are same or not Q: How can we find individual digits of a number ? Ans: For any positive integer, digit at unit place can be obtained by by simply taking mod by 10 That is DigitAtUnitPlace = ( number % 10 ) step 2 and 3 are obvious. Note the following fact 1. When an integer number is divided by 10; fractional part is dropped in result that is, Expression Result 20/10 2 23/10 2 29/10 2 */ class sol_lab4_prob02{ public static void main(String args[]) { // create a counter variable to generate numbers from 1 to 1000 to be tested for Armstrong number int i; // Generate numbers b/w 1 to 1000 for( i = 1 ; i <= 1000 ; i = i + 1) { int curNum; // Number under test int digitAtUnitPlace; int sumOfCubesOfDigitsAtUnitPlace; curNum = i; sumOfCubesOfDigitsAtUnitPlace = 0; while( curNum != 0 ) { digitAtUnitPlace = curNum % 10 ; sumOfCubesOfDigitsAtUnitPlace = sumOfCubesOfDigitsAtUnitPlace + digitAtUnitPlace * digitAtUnitPlace * digitAtUnitPlace ; curNum = curNum / 10; } if ( i == sumOfCubesOfDigitsAtUnitPlace ) { System.out.println(" Armstrong Number :" + i ); } } } }