Studyon Minte9.com
Algorithms (Codingbat)




Number is armstrong



	%java
		package testexample2;

		import org.junit.Assert.*;
		import junit.framework.*;
			
		public class Test extends TestCase {
		    
		    public static void main(String[] args) {
		      
			assertEquals(true, isArmstrong(153));
			assertEquals(false, isArmstrong(123));
			assertEquals(true, isArmstrong(371));
			assertEquals(true, isArmstrong(0));
			assertEquals(true, isArmstrong(-1));
			assertEquals(true, isArmstrong(3));
			assertEquals(false, isArmstrong(99));
			assertEquals(true, isArmstrong(1634));
		    }
		    
		    public static boolean isArmstrong(int n) {
			
			// negative & odd digits
			if (n < 0 && Math.abs(n)%2 == 0) return false;
			
			n = Math.abs(n);
			
			String s = String.valueOf(n);
			int digits = s.length();
			int a = 0;

			for (int i=0; i<digits; i++) {
			    char c = s.charAt(i);
			    int cn = Integer.parseInt(String.valueOf(c));
			    int p = (int) Math.pow(cn, digits);
			    a += p;
			}
			return n == a;
		    }
		}


http://illuminations.nctm.org/LessonDetail.aspx?id=L648
http://java67.blogspot.com/2012/07/java-program-to-find-armstrong-numbers.html