# Print all substring of a number without any conversion

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}

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Take the power of 10 according to size.
2. Divide the number till it will become 0 and print.
3. Then change the number to next the position of that number by taking the modulo with k.
4. Update the no. of digits.
5. Repeat the same process till n become 0.

Below is the implementation of above approach:

## C++

 `// C++ implementation of above approach ` `#include ` `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

 ` `

Output:

```1
12
123
2
23
3
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.