Python 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

Python Compiler
import math combination = 2 num = [4, 8] limit = 500 print("List of combinations of 4 and 8 upto 500:"); #iterate from 1 to limit for i in range(1, limit+1): copyNum = i count = 0 flag = 0 #check each digit starting from last digit while(copyNum != 0): count += 1 lastDigit = copyNum % 10 for j in range(0, combination): if(num[j] == lastDigit): flag += 1 copyNum = math.floor(copyNum / 10) if(count == flag): print(i, end=" ")

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

Python Compiler
import math combination = int(input("Enter the number of Combination: ")) num = [[0]*50 for _ in range(50)] for i in range(0, combination): num[i] = int(input("Enter (int) Digit %d: " % (i+1))) limit = int(input("Enter the limit: ")) #iterate from 1 to limit for i in range(1, limit+1): copyNum = i count = 0 flag = 0 #check each digit starting from last digit while(copyNum != 0): count += 1 lastDigit = copyNum % 10 for j in range(0, combination): if(num[j] == lastDigit): flag += 1 copyNum = math.floor(copyNum / 10) if(count == flag): print(i, end=" ")

Share this Page

Meet the Author