
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all 3 digit repeating numbers in a very large number in C++
In this problem, we are given a number. And we have to print all 3 digit repeating numbers.
Let’s take an example to understand the problem,
Input: 98769876598765 Output: 987: 3 times 876: 3 times 765: 2 times
To solve this problem, we will use the large number which is stored string. The digits of the numbers are counted as characters. Now, we will check the first three numbers and then start from the 3rd index to the end and get a new number. After this, we will check for the next 3 digit numbers are count its frequency. In the end, print all 3 digit numbers that have frequency more than 1.
Example
The below code will implement our solution,
#include <bits/stdc++.h> using namespace std; void printRepeatingNumber(string s) { int i = 0, j = 0, val = 0; map <int, int> threeDigitNumber; val = (s[0] - '0') * 100 + (s[1] - '0') * 10 + (s[2] - '0'); threeDigitNumber[val] = 1; for (i = 3; i < s.length(); i++) { val = (val % 100) * 10 + s[i] - '0'; if (threeDigitNumber.find(val) != threeDigitNumber.end()) { threeDigitNumber[val] = threeDigitNumber[val] + 1; } else { threeDigitNumber[val] = 1; } } for (auto number : threeDigitNumber) { int key = number.first; int value = number.second; if (value > 1) cout<<key<<": "<<value<<" times\n"; } } int main() { string num = "98769876598765"; cout<<"All 3 digit repreating numbers are :\n"; printRepeatingNumber(num); }
Output
All 3 digit repeating numbers are − 765: 2 times 876: 3 times 987: 3 times
- Related Articles
- Print all n-digit strictly increasing numbers in C++
- Find Last Digit of a^b for Large Numbers in C++
- How to handle very large numbers in Python?
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Write all possible 3- digit numbers (without repeating the digits) , by using the digits.(i)6,7,5 (ii) 9,0,2
- Recursive sum of digit in n^x, where n and x are very large in C++
- 3-digit Osiris number in C?
- C# program to print all the numbers divisible by 3 and 5 for a given number
- Number of n digit stepping numbers in C++
- Program to print all the numbers divisible by 3 and 5 in C++
- Print all n digit patterns formed by mobile Keypad in C++
- Print all Good numbers in given range in C++
- Maximum element in a very large array using pthreads in C++
- Find (a^b)%m where ‘a’ is very large in C++
- Print an array with numbers having 1, 2 and 3 as a digit in ascending order

Advertisements