# Count rotations which are divisible by 10

Given a number **N**, the task is to count all the rotations of the given number which are divisible by 10.

**Examples:**

Input:N = 10203

Output:2

Explanation:

There are 5 rotations possible for the given number. They are: 02031, 20310, 03102, 31020, 10203

Out of these rotations, only 20310 and 31020 are divisible by 10. So 2 is the output.

Input:N = 135

Output:0

**Naive Approach:** The naive approach for this problem is to form all the possible rotations. It is known that for a number of size **K**, the number of possible rotations for this number **N** is **K**. Therefore, find all the rotations and for every rotation, check if the number is divisible by 10 or not. The time complexity for this approach is quadratic.

**Efficient Approach:** The efficient approach lies behind the concept that in order to check whether a number is divisible by 10 or not, we simply check if the last digit is 0. So, the idea is to simply iterate over the given number and find the count of 0’s. If the count of 0’s is **F**, then clearly, **F** out of **K** rotations will have 0 at the end of the given number **N**.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// count of rotations which are ` `// divisible by 10 ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// all the rotations which are ` `// divisible by 10. ` `int` `countRotation(` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Loop to iterate through the ` ` ` `// number ` ` ` `do` `{ ` ` ` `int` `digit = n % 10; ` ` ` ` ` `// If the last digit is 0, ` ` ` `// then increment the count ` ` ` `if` `(digit == 0) ` ` ` `count++; ` ` ` `n = n / 10; ` ` ` `} ` `while` `(n != 0); ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 10203; ` ` ` `cout << countRotation(n); ` `} ` |

*chevron_right*

*filter_none*

## C#

`// CSharp implementation to find the ` `// count of rotations which are ` `// divisible by 10 ` ` ` `using` `System; ` `class` `Solution { ` ` ` ` ` `// Function to return the count ` ` ` `// of all rotations which are ` ` ` `// divisible by 10. ` ` ` `static` `int` `countRotation(` `int` `n) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Loop to iterate through the ` ` ` `// number ` ` ` `do` `{ ` ` ` `int` `digit = n % 10; ` ` ` ` ` `// If the last digit is 0, ` ` ` `// then increment the count ` ` ` `if` `(digit % 2 == 0) ` ` ` `count++; ` ` ` `n = n / 10; ` ` ` `} ` `while` `(n != 0); ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 10203; ` ` ` `Console.Write(countRotation(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// count of rotations which are ` `// divisible by 10 ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the count ` ` ` `// of all rotations which are ` ` ` `// divisible by 10. ` ` ` `static` `int` `countRotation(` `int` `n) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Loop to iterate through the ` ` ` `// number ` ` ` `do` `{ ` ` ` `int` `digit = n % ` `10` `; ` ` ` ` ` `// If the last digit is 0, ` ` ` `// then increment the count ` ` ` `if` `(digit == ` `0` `) ` ` ` `count++; ` ` ` `n = n / ` `10` `; ` ` ` `} ` `while` `(n != ` `0` `); ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `10203` `; ` ` ` ` ` `System.out.println(countRotation(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation to find the ` `# count of rotations which are ` `# divisible by 10 ` ` ` `# Function to return the count of ` `# all rotations which are divisible ` `# by 10. ` `def` `countRotation(n): ` ` ` `count ` `=` `0` `; ` ` ` ` ` `# Loop to iterate through the ` ` ` `# number ` ` ` `while` `n > ` `0` `: ` ` ` `digit ` `=` `n ` `%` `10` ` ` ` ` `# If the last digit is 0, ` ` ` `# then increment the count ` ` ` `if` `(digit ` `%` `2` `=` `=` `0` `): ` ` ` `count ` `=` `count ` `+` `1` ` ` `n ` `=` `int` `(n ` `/` `10` `) ` ` ` ` ` `return` `count; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `10203` `; ` ` ` `print` `(countRotation(n)); ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** *O(N)*, where N is the length of the number.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count rotations divisible by 4
- Count rotations divisible by 8
- Count rotations of N which are Odd and Even
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Count of rotations required to generate a sorted array
- Count rotations in sorted and rotated linked list
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count sub-arrays whose product is divisible by k
- Count pairs in array whose sum is divisible by K
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count the numbers divisible by 'M' in a given range
- Count integers in the range [A, B] that are not divisible by C and D
- Count of longest possible subarrays with sum not divisible by K
- Count natural numbers whose factorials are divisible by x but not y
- Count of N digit palindromic numbers divisible by 9
- Count of m digit integers that are divisible by an integer n
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3

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.