Print all substring of a number without any conversion Last Updated : 01 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given an integer N, the task is to print all the substring of N without doing any conversion i.e converting it into a string or an array. Examples: Input: N = 12345 Output: Possible Substrings: {1, 12, 123, 1234, 12345, 2, 23, 234, 2345, 3, 34, 345, 4, 45, 5}Input: N = 123 Output: Possible Substrings: {1, 12, 123, 2, 23, 3} Approach: Take the power of 10 according to size.Divide the number till it will become 0 and print.Then change the number to next the position of that number by taking the modulo with k.Update the no. of digits.Repeat the same process till n becomes 0. Below is the implementation of above approach: C++ // C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to print the substrings of a number void printSubstrings(int n) { // Calculate the total number of digits int s = log10(n); // 0.5 has been added because of it will // return double value like 99.556 int d = (int)(pow(10, s) + 0.5); int k = d; while (n) { // Print all the numbers from // starting position while (d) { cout << n / d << endl; d = d / 10; } // Update the no. n = n % k; // Update the no.of digits k = k / 10; d = k; } } // Driver code int main() { int n = 123; printSubstrings(n); return 0; } Java // Java implementation // of above approach import java.util.*; import java.lang.*; import java.io.*; class GFG { // Function to print the // substrings of a number static void printSubstrings(int n) { // Calculate the total // number of digits int s = (int)Math.log10(n); // 0.5 has been added because // of it will return double // value like 99.556 int d = (int)(Math.pow(10, s) + 0.5); int k = d; while (n > 0) { // Print all the numbers // from starting position while (d > 0) { System.out.println(n / d); d = d / 10; } // Update the no. n = n % k; // Update the no.of digits k = k / 10; d = k; } } // Driver code public static void main(String args[]) { int n = 123; printSubstrings(n); } } // This code is contributed // by Subhadeep Python3 # Python3 implementation of above approach import math # Function to print the substrings of a number def printSubstrings(n): # Calculate the total number of digits s = int(math.log10(n)); # 0.5 has been added because of it will # return double value like 99.556 d = (math.pow(10, s)); k = d; while (n > 0): # Print all the numbers from # starting position while (d > 0): print(int(n // d)); d = int(d / 10); # Update the no. n = int(n % k); # Update the no.of digits k = int(k // 10); d = k; # Driver code if __name__ == '__main__': n = 123; printSubstrings(n); # This code is contributed by Rajput-Ji C# // C# implementation // of above approach using System; class GFG { // Function to print the // substrings of a number static void printSubstrings(int n) { // Calculate the total // number of digits int s = (int)Math.Log10(n); // 0.5 has been added because // of it will return double // value like 99.556 int d = (int)(Math.Pow(10, s) + 0.5); int k = d; while (n > 0) { // Print all the numbers // from starting position while (d > 0) { Console.WriteLine(n / d); d = d / 10; } // Update the no. n = n % k; // Update the no.of digits k = k / 10; d = k; } } // Driver code public static void Main() { int n = 123; printSubstrings(n); } } // This code is contributed // by mits PHP <?php // PHP implementation of above approach // Function to print the substrings // of a number function printSubstrings($n) { // Calculate the total number // of digits $s = (int)log10($n); // 0.5 has been added because // of it will return double // value like 99.556 $d = (int)(pow(10, $s) + 0.5); $k = $d; while ($n) { // Print all the numbers from // starting position while ($d) { echo (int)($n / $d) . "\n"; $d = (int)($d / 10); } // Update the no. $n = $n % $k; // Update the no.of digits $k = (int)($k / 10); $d = $k; } } // Driver code $n = 123; printSubstrings($n); // This code is contributed by mits ?> JavaScript <script> // javascript implementation // of above approach // Function to print the // substrings of a number function printSubstrings(n) { // Calculate the total // number of digits var s = parseInt(Math.log10(n)); // 0.5 has been added because // of it will return double // value like 99.556 var d = parseInt((Math.pow(10, s) + 0.5)); var k = d; while (n > 0) { // Print all the numbers // from starting position while (d > 0) { document.write(parseInt(n / d)+"<br>"); d = parseInt(d / 10); } // Update the no. n = n % k; // Update the no.of digits k = parseInt(k / 10); d = k; } } // Driver code var n = 123; printSubstrings(n); // This code contributed by Princi Singh </script> Output: 1 12 123 2 23 3 Time Complexity: O(nlogn) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Print all substring of a number without any conversion J jaysrm Follow Improve Article Tags : Strings DSA Practice Tags : Strings Similar Reads Program to print ASCII Value of all digits of a given number Given an integer N, the task is to print the ASCII value of all digits of N. Examples: Input: N = 8Output: 8 (56)Explanation:ASCII value of 8 is 56 Input: N = 240Output:2 (50)4 (52)0 (48) Approach: Using the ASCII table shown below, the ASCII value of all the digits of N can be printed: DigitASCII V 5 min read Convert all substrings of length 'k' from base 'b' to decimal A string defining a valid number is given. Output all the base conversions of substrings of length 'k' from base 'b' to base 10. Examples: Input : str = "12212", k = 3, b = 3. Output : 17 25 23 Explanation : All the substrings of length 'k' are : 122, 221, 212. Base conversion can be computed using 10 min read Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space) Given a string representing a number, we need to get the sum of all possible sub strings of this string.Examples : Input: s = "6759"Output: 8421Explanation: sum = 6 + 7 + 5 + 9 + 67 + 75 + 59 + 675 + 759 + 6759 = 8421Input: s = "16"Output: 23Explanation: sum = 1 + 6 + 16 = 23ApproachThe main idea is 6 min read Sum of all substrings of a string representing a number | Set 1 Given an integer represented as a string, we need to get the sum of all possible substrings of this string.Note: It is guaranteed that sum of all substring will fit within a 32-bit integer.Examples: Input: s = "6759"Output: 8421Explanation: sum = 6 + 7 + 5 + 9 + 67 + 75 + 59 + 675 + 759 + 6759 = 842 14 min read Binary representation of a given number Given an integer n, the task is to print the binary representation of the number. Note: The given number will be maximum of 32 bits, so append 0's to the left if the result string is smaller than 30 length.Examples: Input: n = 2Output: 00000000000000000000000000000010Input: n = 0Output: 000000000000 6 min read Print all Substrings of length n possible from the given String Given a string str and an integer N, the task is to print all possible sub-strings of length N. Examples: Input: str = âgeeksforgeeksâ, N = 3Output: gee eek eks ksf sfo for org rge gee eek eksExplanations: All possible sub-strings of length 3 are âgeeâ, âeekâ, âeksâ, âksfâ, âsfoâ, âforâ, âorgâ, ârge 8 min read Remove leading zeros from a Number given as a string Given numeric string str, the task is to remove all the leading zeros from a given string. If the string contains only zeros, then print a single "0". Examples: Input: str = "0001234" Output: 1234 Explanation: Removal of leading substring "000" modifies the string to "1234". Hence, the final answer 7 min read Program to convert a given number to words | Set 2 Write code to convert a given number into words. Examples: Input: 438237764Output: forty three crore eighty two lakh thirty seven thousand seven hundred and sixty four Input: 999999Output: nine lakh ninety nine thousand nine hundred and ninety nine Input: 1000Output: one thousand Explanation: 1000 i 13 min read Move all digits to the beginning of a given string Given a string S, the task is to move all the digits present in the string, to the beginning of the string. Examples: Input: S = âGeeks4forGeeks123âOutput: 4123GeeksforGeeksExplanation:The given string contains digits 4, 1, 2, and 3. Moving all the digits to the beginning of the string modifies the 7 min read Smallest expression to represent a number using single digit Given a number N and a digit D, we have to form an expression or equation that contains only D and that expression evaluates to N. Allowed operators in an expression are +, -, *, and / . Find the minimum length expression that satisfies the condition above and D can only appear in the expression at 15+ min read Like