Program for Armstrong Numbers |
public class armstrongNumber { public void isArmstrong ( String n ) { char [ ] s = n . toCharArray ( ) ; int size = s . length ; int sum = 0 ; for ( char num : s ) { int temp = 1 ; int i = Integer . parseInt ( Character . toString ( num ) ) ; for ( int j = 0 ; j <= size - 1 ; j ++ ) { temp *= i ; } sum += temp ; } if ( sum == Integer . parseInt ( n ) ) { System . out . println ( " True " ) ; } else { System . out . println ( " False " ) ; } } public static void main ( String [ ] args ) { armstrongNumber am = new armstrongNumber ( ) ; am . isArmstrong ( "2" ) ; am . isArmstrong ( "153" ) ; am . isArmstrong ( "1634" ) ; am . isArmstrong ( "231" ) ; } }