C++ Program to Rotate digits of a given number by K Last Updated : 27 Jan, 2022 Comments Improve Suggest changes Like Article Like Report Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required output is 34512 Input: N = 12345, K = -3Output: 34512 Explanation: Right rotating N(= 12345) by K( = -3) modifies N to 34512. Therefore, the required output is 34512 Approach: Follow the steps below to solve the problem: Initialize a variable, say X, to store the count of digits in N.Update K = (K + X) % X to reduce it to a case of left rotation.Remove the first K digits of N and append all the removed digits to the right of the digits of N.Finally, print the value of N. Below is the implementation of the above approach: C++ // C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the count of // digits in N int numberOfDigit(int N) { // Stores count of // digits in N int digit = 0; // Calculate the count // of digits in N while (N > 0) { // Update digit digit++; // Update N N /= 10; } return digit; } // Function to rotate the digits of N by K void rotateNumberByK(int N, int K) { // Stores count of digits in N int X = numberOfDigit(N); // Update K so that only need to // handle left rotation K = ((K % X) + X) % X; // Stores first K digits of N int left_no = N / (int)(pow(10, X - K)); // Remove first K digits of N N = N % (int)(pow(10, X - K)); // Stores count of digits in left_no int left_digit = numberOfDigit(left_no); // Append left_no to the right of // digits of N N = (N * (int)(pow(10, left_digit))) + left_no; cout << N; } // Driver code int main() { int N = 12345, K = 7; // Function Call rotateNumberByK(N, K); return 0; } // The code is contributed by Dharanendra L V Output: 34512 Time Complexity: O(log10N)Auxiliary Space: O(1) Please refer complete article on Rotate digits of a given number by K for more details! Comment More infoAdvertise with us Next Article C++ Program to Rotate digits of a given number by K K kartik Follow Improve Article Tags : Mathematical C++ Programs C++ DSA number-digits rotation +2 More Practice Tags : CPPMathematical Similar Reads C++ Program to Rotate bits of a number Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end. In left rotation, the bits that fall off at left end are put back at right end. In right rotation, the bits that fall off at right end are put ba 3 min read C++ Program to Find Maximum value possible by rotating digits of a given number Given a positive integer N, the task is to find the maximum value among all the rotations of the digits of the integer N. Examples: Input: N = 657Output: 765Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765. Input: N = 7092Output: 9270Explanati 2 min read Program to delete Nth digit of a Number Given a number num and a number n, the task is to delete this nth digit of the number num, from starting and from end.Examples: Input: num = 1234, n = 3 Output: num_after_deleting_from_starting = 124, num_after_deleting_from_end = 134Input: num = 4516312, n = 2 Output: num_after_deleting_from_starti 15+ min read C++ Program for Smallest K digit number divisible by X Integers X and K are given. The task is to find the smallest K-digit number divisible by X. Examples: Input : X = 83, K = 5 Output : 10043 10040 is the smallest 5 digit number that is multiple of 83. Input : X = 5, K = 2 Output : 10 An efficient solution would be : Compute MIN : smallest K-digit num 2 min read C++ Program to Rotate a Matrix by 180 degree Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input : 1 2 3 4 5 6 7 8 9 Output : 9 8 7 6 5 4 3 2 1 Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 Method: 1 (Only prints ro 6 min read Like