Greatest odd factor of an even number | C # program for the above approach ; Function to print greatest odd factor ; Initialize i with 1 ; Iterate till i <= pow_2 ; Find the pow ( 2 , i ) ; If factor is odd , then print the number and break ; Driver code ; Given number ; Function call
using System ; class GFG { public static int greatestOddFactor ( int n ) { int pow_2 = ( int ) ( Math . Log ( n ) ) ; int i = 1 ; while ( i <= pow_2 ) { int fac_2 = ( 2 * i ) ; if ( n % fac_2 == 0 ) { if ( ( n / fac_2 ) % 2 == 1 ) { return ( n / fac_2 ) ; } } i += 1 ; } return 0 ; } public static void Main ( String [ ] args ) { int N = 8642 ; Console . WriteLine ( greatestOddFactor ( N ) ) ; } }