C Program to find Number Combination

Last Updated:

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

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

Share this Page

Meet the Author