Find all Autobiographical Numbers with given number of digits | Java implementation to find Autobiographical numbers with length N ; Converting the integer number to string ; Extracting each character from each index one by one and converting into an integer ; initialize count as 0 ; Check if it is equal to the index i if true then increment the count ; It is an Autobiographical number ; Return false if the count and the index number are not equal ; Function to print autobiographical number with given number of digits ; both the boundaries are taken double , so as to satisfy Math . pow ( ) function 's signature ; Left boundary of interval ; Right boundary of interval ; Flag = 0 implies that the number is not an autobiographical no . ; Driver Code
import java . util . * ; import java . lang . Math ; public class autobio { public static boolean isAutoBio ( int num ) { String autoStr ; int index , number , i , j , cnt ; autoStr = Integer . toString ( num ) ; for ( i = 0 ; i < autoStr . length ( ) ; i ++ ) { index = Integer . parseInt ( autoStr . charAt ( i ) + " " ) ; cnt = 0 ; for ( j = 0 ; j < autoStr . length ( ) ; j ++ ) { number = Integer . parseInt ( autoStr . charAt ( j ) + " " ) ; if ( number == i ) cnt ++ ; } if ( cnt != index ) return false ; } return true ; } public static void findAutoBios ( double n ) { double high , low ; int i , flag = 0 ; low = Math . pow ( 10.0 , n - 1 ) ; high = Math . pow ( 10.0 , n ) - 1.0 ; for ( i = ( int ) low ; i <= ( int ) high ; i ++ ) if ( isAutoBio ( i ) ) { flag = 1 ; System . out . print ( i + " , ▁ " ) ; } if ( flag == 0 ) System . out . println ( " There ▁ is ▁ no ▁ Autobiographical ▁ Number " + " with ▁ " + ( int ) n + " ▁ digits " ) ; } public static void main ( String [ ] args ) { double N = 0 ; findAutoBios ( N ) ; N = 4 ; findAutoBios ( N ) ; } }