C++ Program to find Number Combination

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; }

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; }

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author