- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- C program to print number series without using any loop
- Print a pattern without using any loop in C++
- 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
- Print “Hello World” without using any header file in C
- C program to print a string without any quote in the program
- Print m multiplies of n without using any loop in Python.
- Print all Prime Quadruplet of a number less than it in C++
- Add number strings without using conversion library methods in JavaScript
- Print first m multiples of n without using any loop in Python
- Maximum Number of Occurrences of a Substring in C++
- Print all combinations of points that can compose a given number in C++
- How to print a large number of values in R without index?
- C Program for efficiently print all prime factors of a given number?

Advertisements