VOOZH about

URL: https://www.geeksforgeeks.org/java/java-program-to-generate-all-rotations-of-a-number/

⇱ Java Program to Generate all rotations of a number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Generate all rotations of a number

Last Updated : 23 Jul, 2025

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 312
Input: 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: 


Output
4451 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
Article Tags: