C++ Program to Count rotations which are divisible by 10 Last Updated : 17 Aug, 2023 Comments Improve Suggest changes Like Article Like Report 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); } Output: 2 Time Complexity: O(log10N), where N is the length of the number.Auxiliary Space: O(1) Please refer complete article on Count rotations which are divisible by 10 for more details! Comment More infoAdvertise with us Next Article C++ Program to Count rotations which are divisible by 10 K kartik Follow Improve Article Tags : C++ rotation Numbers Number Divisibility Practice Tags : CPPNumbers Similar Reads Javascript Program to 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 d 2 min read 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 d 4 min read Javascript Program to Count rotations divisible by 4 Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8Output: 1Input: 20Output: 1Rotation: 20 is divisible by 4 02 is not divisible by 4Input : 13502Output : 0No rotation is divisible by 4Input : 43292816Output : 55 rotations are 2 min read Javascript Program to Count rotations divisible by 8 Given a large positive number as string, count all rotations of the given number which are divisible by 8.Examples: Input: 8Output: 1Input: 40Output: 1Rotation: 40 is divisible by 8 04 is not divisible by 8Input : 13502Output : 0No rotation is divisible by 8Input : 43262488612Output : 4Approach: It 3 min read Count rotations divisible by 8 Given a large positive number as string, count all rotations of the given number which are divisible by 8. Examples: Input: 8 Output: 1 Input: 40 Output: 1 Rotation: 40 is divisible by 8 04 is not divisible by 8 Input : 13502 Output : 0 No rotation is divisible by 8 Input : 43262488612 Output : 4 Ap 10 min read Like