Find all Autobiographical Numbers with given number of digits | C ++ implementation to find Autobiographical numbers with length N ; Function to return if the number is autobiographical or not ; Converting the integer number to string ; Extracting each character from each index one by one and converting into an integer ; Initialise 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 ; Left boundary of interval ; Right boundary of interval ; Flag = 0 implies that the number is not an autobiographical no . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isAutoBio ( int num ) { string autoStr ; int index , number , i , j , cnt ; autoStr = to_string ( num ) ; for ( int i = 0 ; i < autoStr . size ( ) ; i ++ ) { index = autoStr . at ( i ) - '0' ; cnt = 0 ; for ( j = 0 ; j < autoStr . size ( ) ; j ++ ) { number = autoStr . at ( j ) - '0' ; if ( number == i ) cnt ++ ; } if ( index != cnt ) return false ; } return true ; } void findAutoBios ( int n ) { int high , low , i , flag = 0 ; low = pow ( 10 , n - 1 ) ; high = pow ( 10 , n ) - 1 ; for ( i = low ; i <= high ; i ++ ) { if ( isAutoBio ( i ) ) { flag = 1 ; cout << i << " , ▁ " ; } } if ( ! flag ) cout << " There ▁ is ▁ no ▁ " << " Autobiographical ▁ number " << " ▁ with ▁ " << n << " ▁ digits STRNEWLINE " ; } int main ( ) { int N = 0 ; findAutoBios ( N ) ; N = 4 ; findAutoBios ( N ) ; return 0 ; }