Python 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
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=" ")
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
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=" ")
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 Python with examples for quick and easy learning.
We are working to cover every Single Concept in Python.
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.