

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print all substring of a number without any conversion in C++
In this problem, we are given an integer n. And we have to print all substrings of a number that can be formed but converting the string are not allowed i.e. we cannot convert an integer into string or array.
Let’s take an example to understand the topic better −
Input: number =5678 Output: 5, 56, 567, 5678, 6, 67, 678, 7, 78, 8
In order to solve this problem, we will need to use mathematical logic. Here, we will print the most significant bit first, then successive bits are printed.
Algorithm
Step1: Take a 10’s power number based on the number of digits. Step2: print values recursively and divide the number by 10 and repeat until the number becomes 0. Step3: Eliminate the MSB of the number and repeat step 2 with this number. Step4: Repeat till the number becomes 0.
Example
#include <iostream> #include<math.h> using namespace std; void printSubNumbers(int n) ; int main(){ int n = 6789; cout<<"The number is "<<n<<" and the substring of number are :\n"; printSubNumbers(n); return 0; } void printSubNumbers(int n){ int s = log10(n); int d = (int)(pow(10, s) + 0.5); int k = d; while (n) { while (d) { cout<<(n / d)<<" "; d = d / 10; } n = n % k; k = k / 10; d = k; } }
Output
The number is 6789 and the substring of a number are −
6 67 678 6789 7 78 789 8 89 9
- Related Questions & Answers
- Print Number series without using any loop in Python Program
- Python Program for Print Number series without using any loop
- Java Program to print Number series without using any loop
- C program to print number series without using any loop
- Print a pattern without using any loop in C++
- Print m multiplies of n without using any loop in Python.
- Print first m multiples of n without using any loop in Python
- C program to print a string without any quote in the program
- Add number strings without using conversion library methods in JavaScript
- How to print a large number of values in R without index?
- Java program to print a multiplication table for any number
- Python Program to Print Numbers in a Range (1,upper) Without Using any Loops
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Print “Hello World” without using any header file in C
- Golang Program to Print the Numbers in a Range (1, upper) without Using any Loops
Advertisements