Generate all rotations of a number Last Updated : 23 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: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count of digits of n int numberOfDigits(int n) { int cnt = 0; while (n > 0) { cnt++; n /= 10; } return cnt; } // Function to print the left shift numbers void cal(int num) { int digits = numberOfDigits(num); int powTen = 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); cout << left << " "; // Update the original number num = left; } } // Driver Code int main() { int num = 1445; cal(num); return 0; } 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 Python3 # Python3 implementation of the approach # function to return the count of digit of n def numberofDigits(n): cnt = 0 while n > 0: cnt += 1 n //= 10 return cnt # function to print the left shift numbers def cal(num): digit = numberofDigits(num) powTen = pow(10, digit - 1) for i in range(digit - 1): firstDigit = num // powTen # formula to calculate left shift # from previous number left = (num * 10 + firstDigit - (firstDigit * powTen * 10)) print(left, end = " ") # Update the original number num = left # Driver code num = 1445 cal(num) # This code is contributed # by Mohit Kumar C# // C# implementation of the approach using System; public 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); Console.Write(left + " "); // Update the original number num = left; } } // Driver Code static public void Main (){ int num = 1445; cal(num); } } // This code is contributed by akt_mit.... PHP <?php // PHP implementation of the approach // Function to return the count // of digits of n function numberOfDigits($n) { $cnt = 0; while ($n > 0) { $cnt++; $n = floor($n / 10); } return $cnt; } // Function to print the left shift numbers function cal($num) { $digits = numberOfDigits($num); $powTen = pow(10, $digits - 1); for ($i = 0; $i < $digits - 1; $i++) { $firstDigit = floor($num / $powTen); // Formula to calculate left shift // from previous number $left = (($num * 10) + $firstDigit) - ($firstDigit * $powTen * 10); echo $left, " "; // Update the original number $num = $left; } } // Driver Code $num = 1445; cal($num); // This code is contributed by Ryuga ?> JavaScript <script> // Javascript implementation of the approach // Function to return the count of digits of n function numberOfDigits(n) { let cnt = 0; while (n > 0) { cnt++; n = parseInt(n / 10, 10); } return cnt; } // Function to print the left shift numbers function cal(num) { let digits = numberOfDigits(num); let powTen = Math.pow(10, digits - 1); for (let i = 0; i < digits - 1; i++) { let firstDigit = parseInt(num / powTen, 10); // Formula to calculate left shift // from previous number let left = ((num * 10) + firstDigit) - (firstDigit * powTen * 10); document.write(left + " "); // Update the original number num = left; } } let num = 1445; cal(num); </script> Output: 4451 4514 5144 Time Complexity: O(log10n)Auxiliary Space: O(1), since no extra space has been taken. Comment More infoAdvertise with us Next Article Interview Preparation For Software Developers K krikti Follow Improve Article Tags : Misc Mathematical DSA Akamai number-digits rotation +2 More Practice Tags : MathematicalMisc Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like