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++
// 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
filter_none
Java
// 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
filter_none
Python3
# 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
filter_none
C#
// 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
filter_none
PHP
<?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
filter_none
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++
// 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
filter_none
Java
// 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
filter_none
Python3
# 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
filter_none
C#
// 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
filter_none
PHP
<?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
filter_none
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.
Recommended Posts:
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Minimum positive integer divisible by C and is not in range [A, B]
- Minimum number of changes such that elements are first Negative and then Positive
- Print first k digits of 1/n where n is a positive integer
- Maximum positive integer divisible by C and is in the range [A, B]
- Minimum Possible sum of digits in a positive multiple of N
- Divide 1 to n into two groups with minimum sum difference
- Minimum cuts required to divide the Circle into equal parts
- Minimum numbers needed to express every integer below N as a sum
- Find the number of ways to divide number into four parts such that a = c and b = d
- Count number of ways to divide a number in 4 parts
- Find count of digits in a number that divide the number
- Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [2, N]
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- Sum and Product of digits in a number that divide the number



