# C++ Program to find Number Combination

You are Here:

## Find Number Combination

In the following example, we will find all possible combinations of numbers with 4 and 8 within the limit 500.

### Example

C++ Compiler
#include <iostream> using namespace std; int main() { const int combination = 2; int num[combination] = {4, 8}; int limit = 500; int i, j, lastDigit, copyNum, digit, count; cout << "List of combinations of 4 and 8 upto 500:\n"; // Iterate from 1 to limit for(i=1; i<=limit; i++) { copyNum = i; count = 0; digit = 0; // Check each digit starting from last digit while(copyNum != 0) { count++; lastDigit = copyNum % 10; for(j=0; j<combination; j++) { if(num[j] == lastDigit) digit++; } copyNum = copyNum / 10; } // result if(count == digit) cout << i << " "; } return 0; }

### Output

List of combinations of 4 and 8 upto 500: 4 8 44 48 84 88 444 448 484 488

## Find Number Combination for any Given Numbers

In the following example, we will find all possible combinations of the given numbers within the given limit.

### Example

C++ Compiler
#include <iostream> using namespace std; int main() { int i, j, lastDigit, copyNum, digit, count; int num[50], combination, limit; cout << "Enter the number of Combination: "; cin >> combination; for(i=0; i<combination; i++) { cout << "Enter (int) Digit " << i+1 << ": "; cin >> num[i]; } cout << "Enter the Limit: "; cin >> limit; // Iterate from 1 to limit for(i=1; i<=limit; i++) { copyNum = i; count = 0; digit = 0; // Check each digit starting from last digit while(copyNum != 0) { count++; lastDigit = copyNum % 10; for(j=0; j<combination; j++) { if(num[j] == lastDigit) digit++; } copyNum = copyNum / 10; } // result if(count == digit) cout << i << " "; } return 0; }

### Output

Enter the number of Combination: 2 Enter (int) Digit 1: 1 Enter (int) Digit 2: 2 Enter the Limit: 500 1 2 11 12 21 22 111 112 121 122 211 212 221 222

## Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on C++ with examples for quick and easy learning.

We are working to cover every Single Concept in C++.