Java Program to Generate all rotations of a number Last Updated : 09 Jun, 2022 Comments Improve Suggest changes Like Article Like Report Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 312Input: n = 1445 Output: 4451 4514 5144 Approach: Assume n = 123.Multiply n with 10 i.e. n = n * 10 = 1230.Add the first digit to the resultant number i.e. 1230 + 1 = 1231.Subtract (first digit) * 10k from the resultant number where k is the number of digits in the original number (in this case, k = 3).1231 - 1000 = 231 is the left shift number of the original number. Below is the implementation of the above approach: Java // Java implementation of the approach class GFG { // Function to return the count of digits of n static int numberOfDigits(int n) { int cnt = 0; while (n > 0) { cnt++; n /= 10; } return cnt; } // Function to print the left shift numbers static void cal(int num) { int digits = numberOfDigits(num); int powTen = (int) Math.pow(10, digits - 1); for (int i = 0; i < digits - 1; i++) { int firstDigit = num / powTen; // Formula to calculate left shift // from previous number int left = ((num * 10) + firstDigit) - (firstDigit * powTen * 10); System.out.print(left + " "); // Update the original number num = left; } } // Driver Code public static void main(String[] args) { int num = 1445; cal(num); } } // This code is contributed by // PrinciRaj1992 Output4451 4514 5144 Time Complexity: O(log10(num))Auxiliary Space: O(1) Please refer complete article on Generate all rotations of a number for more details! Comment More infoAdvertise with us Next Article Java Program to Generate all rotations of a number K kartik Follow Improve Article Tags : Java Mathematical Java Programs DSA Akamai number-digits rotation +3 More Practice Tags : JavaMathematical Similar Reads Java Program to Rotate bits of a number Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end. In the left rotation, the bits that fall off at the left end are put back at the right end. In the right rotation, the bits that fall off at th 3 min read Java Program to Rotate digits of a given number by K 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 2 min read Java Program to Print all possible rotations of a given Array Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat 3 min read Java Program to Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3} Output: 2 Explanation: Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2} Output: 1 Explanation: So 4 min read Java Program to Left Rotate the Elements of an Array In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java.Example: We can use a temporary array to rotate the array left by " 5 min read Like