Nearest smaller power of 2 for every digit of a number | C # program to implement the above approach ; Function to find the nearest power of two for every digit of a given number ; Converting number to string ; Traverse the array ; Calculate log base 2 of the current digit s [ i ] ; Highest power of 2 <= s [ i ] ; ASCII conversion ; Driver Code
using System ; class GFG { static void highestPowerOfTwo ( int num ) { String s = num . ToString ( ) ; for ( int i = 0 ; i < ( int ) s . Length ; i ++ ) { if ( s [ i ] == '0' ) { Console . Write ( "0" ) ; continue ; } int lg = ( int ) ( Math . Log ( s [ i ] - '0' ) / Math . Log ( 2 ) ) ; int p = ( int ) Math . Pow ( 2 , lg ) ; Console . Write ( ( char ) ( p + 48 ) ) ; } } public static void Main ( ) { int num = 4317 ; highestPowerOfTwo ( num ) ; } }