Rotate digits of a given number by K - GeeksforGeeks

29 Apr, 2021
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++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to find the count of// digits in Nint 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 Kvoid 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 codeint main(){    int N = 12345, K = 7;     // Function Call    rotateNumberByK(N, K);    return 0;} // The code is contributed by Dharanendra L V
// Java program to implement// the above approach import java.io.*; class GFG {     // Function to find the count of    // digits in N    static 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    static 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)(Math.pow(10,                                         X - K));         // Remove first K digits of N        N = N % (int)(Math.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)(Math.pow(10, left_digit)))            + left_no;         System.out.println(N);    }     // Driver Code    public static void main(String args[])    {         int N = 12345, K = 7;         // Function Call        rotateNumberByK(N, K);    }}
# Python3 program to implement# the above approach # Function to find the count of# digits in Ndef numberOfDigit(N):     # Stores count of    # digits in N    digit = 0     # Calculate the count    # of digits in N    while (N > 0):         # Update digit        digit += 1         # Update N        N //= 10    return digit # Function to rotate the digits of N by Kdef rotateNumberByK(N, K):     # Stores count of digits in N    X = numberOfDigit(N)     # Update K so that only need to    # handle left rotation    K = ((K % X) + X) % X     # Stores first K digits of N    left_no = N // pow(10, X - K)     # Remove first K digits of N    N = N % pow(10, X - K)     # Stores count of digits in left_no    left_digit = numberOfDigit(left_no)     # Append left_no to the right of    # digits of N    N = N * pow(10, left_digit) + left_no    print(N) # Driver Codeif __name__ == '__main__':    N, K = 12345, 7     # Function Call    rotateNumberByK(N, K)     # This code is contributed by mohit kumar 29
// C# program to implement// the above approachusing System;class GFG{     // Function to find the count of    // digits in N    static 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    static 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)(Math.Pow(10,                                         X - K));         // Remove first K digits of N        N = N % (int)(Math.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)(Math.Pow(10, left_digit)))            + left_no;         Console.WriteLine(N);    }     // Driver Code    public static void Main(string []args)    {         int N = 12345, K = 7;         // Function Call        rotateNumberByK(N, K);    }} // This code is contributed by AnkThon
<script>// Javascript program to implement// the above approach     // Function to find the count of    // digits in N    function numberOfDigit(N)    {          // Stores count of        // digits in N        let digit = 0;          // Calculate the count        // of digits in N        while (N > 0) {              // Update digit            digit++;              // Update N            N = Math.floor( N / 10);        }        return digit;    }      // Function to rotate the digits of N by K    function rotateNumberByK(N, K)    {          // Stores count of digits in N        let X = numberOfDigit(N);          // Update K so that only need to        // handle left rotation        K = ((K % X) + X) % X;          // Stores first K digits of N        let left_no = Math.floor (N / Math.floor(Math.pow(10,                                         X - K)));          // Remove first K digits of N        N = N % Math.floor(Math.pow(10, X - K));          // Stores count of digits in left_no        let left_digit = numberOfDigit(left_no);          // Append left_no to the right of        // digits of N        N = (N * Math.floor(Math.pow(10, left_digit)))            + left_no;          document.write(N);    } // Driver Code     let N = 12345, K = 7;          // Function Call        rotateNumberByK(N, K);     // This code is contributed by souravghosh0416.</script>
34512
 
Time Complexity: O(log10N)Auxiliary Space: O(1)
mohit kumar 29
ankthon
dharanendralv23
souravghosh0416
number-digits
rotation
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program for factorial of a number
Print all possible combinations of r elements in a given array of size n
Program for Decimal to Binary Conversion
The Knight's tour problem | Backtracking-1
Operators in C / C++
Find minimum number of coins that make a given value
Program to find sum of elements in a given array