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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int greatestOddFactor ( int n ) { int pow_2 = ( int ) ( 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 ; } } int main ( ) { int N = 8642 ; cout << greatestOddFactor ( N ) ; return 0 ; }