Print all substring of a number without any conversion - GeeksforGeeks

25 Oct, 2021
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.
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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to print the substrings of a numbervoid 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 codeint main(){    int n = 123;    printSubstrings(n);     return 0;}
// Java implementation// of above approachimport java.util.*;import java.lang.*;import java.io.*; class GFG{// Function to print the// substrings of a numberstatic 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 codepublic static void main(String args[]){    int n = 123;    printSubstrings(n);}} // This code is contributed// by Subhadeep
# Python3 implementation of above approachimport math # Function to print the substrings of a numberdef 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 codeif __name__ == '__main__':    n = 123;    printSubstrings(n); # This code is contributed by Rajput-Ji
// C# implementation// of above approachusing System; class GFG{// Function to print the// substrings of a numberstatic 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 codepublic static void Main(){    int n = 123;    printSubstrings(n);}} // This code is contributed// by mits
<?php// PHP implementation of above approach // Function to print the substrings// of a numberfunction 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?>
<script> // javascript implementation// of above approach// Function to print the// substrings of a numberfunction 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>
1
12
123
2
23
3
 
tufan_gupta2000
Mithun Kumar
Rajput-Ji
princi singh
ankita_saini
maths-log
Modular Arithmetic
substring
Mathematical
Mathematical
Modular Arithmetic
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 to find GCD or HCF of two numbers
Sieve of Eratosthenes
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
Program for factorial of a number
Find minimum number of coins that make a given value