Given a number **n**, the task is to count all rotations of the given number which are odd and even.

**Examples:**

Input:n = 1234Output:Odd = 2, Even = 2 Total rotations: 1234, 2341, 3412, 4123 Odd rotations: 2341 and 4123 Even rotations: 1234 and 3412Input:n = 246Output:Odd = 0, Even = 3

**Efficient Approach**: For large numbers, it is difficult to rotate and check whether it is odd or not for every rotation. Hence, in this approach, check the count of odd digits and even digits present in the number. These will be the answer to this problem.

Below is the implementation of the above approach:

**Implementation:**

## C++

`// C++ implementation of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count of all rotations ` `// which are odd and even ` `void` `countOddRotations(` `int` `n) ` `{ ` ` ` `int` `odd_count = 0, even_count = 0; ` ` ` `do` `{ ` ` ` `int` `digit = n % 10; ` ` ` `if` `(digit % 2 == 1) ` ` ` `odd_count++; ` ` ` `else` ` ` `even_count++; ` ` ` `n = n / 10; ` ` ` `} ` `while` `(n != 0); ` ` ` ` ` `cout << ` `"Odd = "` `<< odd_count << endl; ` ` ` `cout << ` `"Even = "` `<< even_count << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 1234; ` ` ` `countOddRotations(n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `class` `Solution { ` ` ` ` ` `// Function to count of all rotations ` ` ` `// which are odd and even ` ` ` `static` `void` `countOddRotations(` `int` `n) ` ` ` `{ ` ` ` `int` `odd_count = ` `0` `, even_count = ` `0` `; ` ` ` `do` `{ ` ` ` `int` `digit = n % ` `10` `; ` ` ` `if` `(digit % ` `2` `== ` `1` `) ` ` ` `odd_count++; ` ` ` `else` ` ` `even_count++; ` ` ` `n = n / ` `10` `; ` ` ` `} ` `while` `(n != ` `0` `); ` ` ` ` ` `System.out.println(` `"Odd = "` `+ odd_count); ` ` ` `System.out.println(` `"Even = "` `+ even_count); ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `1234` `; ` ` ` `countOddRotations(n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the above approach ` ` ` `# Function to count of all rotations ` `# which are odd and even ` `def` `countOddRotations(n): ` ` ` `odd_count ` `=` `0` `; even_count ` `=` `0` ` ` `while` `n !` `=` `0` `: ` ` ` `digit ` `=` `n ` `%` `10` ` ` `if` `digit ` `%` `2` `=` `=` `0` `: ` ` ` `odd_count ` `+` `=` `1` ` ` `else` `: ` ` ` `even_count ` `+` `=` `1` ` ` `n ` `=` `n` `/` `/` `10` ` ` `print` `(` `"Odd ="` `, odd_count) ` ` ` `print` `(` `"Even ="` `, even_count) ` ` ` `# Driver code ` `n ` `=` `1234` `countOddRotations(n) ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// CSharp implementation of the above approach ` ` ` `using` `System; ` `class` `Solution { ` ` ` ` ` `// Function to count of all rotations ` ` ` `// which are odd and even ` ` ` `static` `void` `countOddRotations(` `int` `n) ` ` ` `{ ` ` ` `int` `odd_count = 0, even_count = 0; ` ` ` `do` `{ ` ` ` `int` `digit = n % 10; ` ` ` `if` `(digit % 2 == 1) ` ` ` `odd_count++; ` ` ` `else` ` ` `even_count++; ` ` ` `n = n / 10; ` ` ` `} ` `while` `(n != 0); ` ` ` ` ` `Console.WriteLine(` `"Odd = "` `+ odd_count); ` ` ` `Console.WriteLine(` `"Even = "` `+ even_count); ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 1234; ` ` ` `countOddRotations(n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Function to count of all rotations ` `// which are odd and even ` `function` `countOddRotations(` `$n` `) ` `{ ` ` ` `$odd_count` `= 0; ` ` ` `$even_count` `= 0; ` ` ` `do` `{ ` ` ` `$digit` `= ` `$n` `% 10; ` ` ` `if` `(` `$digit` `% 2 == 1) ` ` ` `$odd_count` `++; ` ` ` `else` ` ` `$even_count` `++; ` ` ` `$n` `= (int)(` `$n` `/ 10); ` ` ` `} ` `while` `(` `$n` `!= 0); ` ` ` ` ` `echo` `"Odd = "` `, ` `$odd_count` `, ` `"\n"` `; ` ` ` `echo` `"Even = "` `, ` `$even_count` `, ` `"\n"` `; ` `} ` ` ` `// Driver Code ` `$n` `= 1234; ` `countOddRotations(` `$n` `); ` ` ` `// This code is contributed by ajit.. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Odd = 2 Even = 2

**Time Complexity:** O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count rotations which are divisible by 10
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Number of strings which starts and ends with same character after rotations
- Count rotations in sorted and rotated linked list
- Count rotations divisible by 4
- Count rotations divisible by 8
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Count of rotations required to generate a sorted array
- Check if count of even divisors of N is equal to count of odd divisors
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Generate an array of given size with equal count and sum of odd and even numbers
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count number of ordered pairs with Even and Odd Sums
- Count number of ordered pairs with Even and Odd Product
- Count Odd and Even numbers in a range from L to R
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of odd and even sum pairs in an array
- Count set bits in the Kth number after segregating even and odd from N natural numbers

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.