Minimum positive integer to divide a number such that the result is an odd

Given a number n. Print the minimum positive integer by which it should be divided so that the result is an odd number.

Examples :

Input : 36
Output : 4
36 must be divided by 4 or 12 to make it odd.
We take minimum of 4 and 12 i.e. 4

Input : 8
Output : 8
8 must be divided by 8 to make it an odd number.

Method 1:



Find the minimum number that makes the given number 
odd  by dividing it one by one from 2(i) to N
If (N/i) is odd then return i.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to make a number odd
#include <iostream>
using namespace std;
  
int makeOdd(int n)
{
    // Return 1 if already odd
    if (n % 2 != 0)
        return 1;
  
    // Check on dividing with a number when
    // the result becomes odd Return that number
    for (int i = 2 ; i <= n ; i++)
  
        // If n is divided by i and n/i is odd
        // then return i
        if ((n % i == 0) && ((n / i) % 2 == 1))
            return i;
  
}
  
// Driver code
int main()
{
    int n = 36;
    cout << makeOdd(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to make a number odd
import java.io.*;
  
class GFG
{
    static int makeOdd(int n)
    {
        // Return 1 if already odd
        if (n % 2 != 0)
            return 1;
  
        // Check on dividing with a number when
        // the result becomes odd Return that number
        int i;
        for (i = 2 ; i <= n ; i++)
  
            // If n is divided by i and n/i is odd
            // then return i
            if ((n % i  == 0) && ((n / i) % 2 == 1))
                break;
  
        return i;
    }
  
    // Driver code
    public static void main (String[] args)
    {
        int n = 36;
        int res = makeOdd(n);
        System.out.println(res);
    }
}
  
// This code is contributed by Pramod Kumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to 
# make a number odd
  
def makeOdd(n):
      
    # Return 1 if 
    # already odd
    if n % 2 != 0:
        return 1;
          
    # Check on dividing 
    # with a number when
    # the result becomes
    # odd Return that number
    for i in range(2, n):
          
        # If n is divided by
        # i and n/i is odd
        # then return i
        if (n % i == 0 and 
           (int)(n / i) % 2 == 1):
            return i;
  
# Driver code
n = 36;
print(makeOdd(n));
  
# This code is contributed 
# by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to make a number odd
using System;
  
class GFG
{
    static int makeOdd(int n)
    {
        // Return 1 if already odd
        if (n % 2 != 0)
            return 1;
  
        // Check on dividing with a number when
        // the result becomes odd Return that number
        int i;
        for (i = 2 ; i <= n ; i++)
  
            // If n is divided by i and n/i is odd
            // then return i
            if ((n % i == 0) && ((n / i) % 2 == 1))
                break;
  
        return i;
    }
  
    // Driver code
    public static void Main ()
    {
        int n = 36;
        int res = makeOdd(n);
        Console.Write(res);
    }
}
  
// This code is contributed by nitin mittal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to make 
// a number odd
  
function makeOdd($n)
{
      
    // Return 1 if already odd
    if ($n % 2 != 0)
        return 1;
  
    // Check on dividing with
    // a number when the result 
    // becomes odd Return that
    // number
    for ($i = 2 ; $i <= $n ; $i++)
  
        // If n is divided by i and
        // n/i is odd then return i
        if (($n % $i == 0) && 
           (($n / $i) % 2 == 1))
            return $i;
  
}
  
    // Driver code
    $n = 36;
    echo makeOdd($n);
      
// This code is contributed by nitin mittal.
?>

chevron_right



Output:

4

Method 2:

 As there is only one case to make a number odd i.e.
It is an even number i.e. divisible by 2.
So, the point is to find the smallest multiple of 2 
that can make the given number odd.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to make a number odd
#include <iostream>
using namespace std;
  
// Function to find the value
int makeOdd(int n)
{
    // Return 1 if already odd
    if (n%2 != 0)
        return 1;
  
    // Check how many times it is divided by 2
    int resul = 1;
    while (n%2 == 0)
    {
        n /= 2;
        resul *= 2;
    }
  
    return resul;
}
  
// Driver code
int main()
{
    int n = 36;
    cout << makeOdd(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to make a number odd
import java.io.*;
  
class GFG
{
    // Function to find the value
    static int makeOdd(int n)
    {
        // Return 1 if already odd
        if (n % 2 != 0)
            return 1;
  
        // Check how many times it is divided by 2
        int ans = 1;
        while (n % 2 == 0)
        {
            n /= 2;
            ans *= 2;
        }
        return ans;
    }
  
    // Driver code
    public static void main (String[] args)
    {
        int n = 36;
        int res = makeOdd(n);
        System.out.println(res);
    }
}
// This code is contributed by Pramod Kumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to
# make a number odd
  
# Function to find 
# the value
def makeOdd(n):
      
    # Return 1 if 
    # already odd
    if (n % 2 != 0):
        return 1;
          
    # Check how many times
    # it is divided by 2
    resul = 1;
    while (n % 2 == 0):
        n = n/ 2;
        resul = resul * 2;
    return resul;
  
# Driver code
n = 36;
print(makeOdd(n));
  
# This code is contributed
# by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to make a number odd
using System;
  
class GFG {
      
    // Function to find the value
    static int makeOdd(int n)
    {
        // Return 1 if already odd
        if (n % 2 != 0)
            return 1;
  
        // Check how many times it 
        // is divided by 2
        int ans = 1;
        while (n % 2 == 0)
        {
            n /= 2;
            ans *= 2;
        }
        return ans;
    }
  
    // Driver code
    public static void Main ()
    {
        int n = 36;
        int res = makeOdd(n);
        Console.Write(res);
    }
}
  
// This code is contributed by
// nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to make
// a number odd
  
// Function to find the value
function makeOdd( $n)
{
    // Return 1 if already odd
    if ($n % 2 != 0)
        return 1;
  
    // Check how many times 
    // it is divided by 2
    $resul = 1;
    while ($n % 2 == 0)
    {
        $n /= 2;
        $resul *= 2;
    }
  
    return $resul;
}
  
// Driver code
$n = 36;
echo makeOdd($n);
  
// This code is contributed by anuj_67.
?>

chevron_right



Output :

4

This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up