MÃ ¼ nchhausen Number | C ++ code for MA14nchhausen Number ; pwr [ i ] is going to store i raised to power i . ; Function to check out whether the number is MA14nchhausen Number or not ; Precompute i raised to power i for every i ; The input here is fixed i . e . it will check up to n ; check the integer for MA14nchhausen Number , if yes then print out the number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned pwr [ 10 ] ; bool isMunchhausen ( unsigned n ) { unsigned sum = 0 ; int temp = n ; while ( temp ) { sum += pwr [ ( temp % 10 ) ] ; temp /= 10 ; } return ( sum == n ) ; } void printMunchhausenNumbers ( int n ) { for ( int i = 0 ; i < 10 ; i ++ ) pwr [ i ] = ( unsigned ) pow ( ( float ) i , ( float ) i ) ; for ( unsigned i = 1 ; i <= n ; i ++ ) if ( isMunchhausen ( i ) ) cout << i << " STRNEWLINE " ; } int main ( ) { int n = 10000 ; printMunchhausenNumbers ( n ) ; return 0 ; }