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 <stdio.h> #define combination 2 int main() { int num[combination] = {4, 8}; int limit = 500; int i, j, lastDigit, copyNum, flag, count; printf("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; flag = 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) flag++; } copyNum = copyNum / 10; } // result if(count == flag) printf("%d ", 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 <stdio.h> int main() { int num[50], combination, limit; int i, j, lastDigit, copyNum, flag, count; printf("Enter the number of Combination: "); scanf("%d", &combination); for(i=0; i<combination; i++) { printf("Enter (int) Digit %d: ", i+1); scanf("%d", &num[i]); } printf("Enter the Limit: "); scanf("%d", &limit); // Iterate from 1 to limit for(i=1; i<=limit; i++) { copyNum = i; count = 0; flag = 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) flag++; } copyNum = copyNum / 10; } // result if(count == flag) printf("%d ", 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 98% 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.

Please do google search for:

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