Python Program to Print All Possible Combinations of Three Digits

This is a Python Program to accept three distinct digits and print all possible combinations from the digits.

Problem Description

The program takes three distinct numbers and prints all possible combinations from the digits.

Problem Solution

1. Take in the first, second and third number and store it in separate variables.
2. Then append all the three numbers to the list.
3. Use three for loops and print the digits in the list if none of their indexes are equal to each other.
4. Exit.

Program/Source Code

Here is the source code of the Python Program to accept three distinct digits and prints all possible combinations from the digits. The program output is also shown below.

 
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter third number:"))
d=[]
d.append(a)
d.append(b)
d.append(c)
for i in range(0,3):
    for j in range(0,3):
        for k in range(0,3):
            if(i!=j&j!=k&k!=i):
                print(d[i],d[j],d[k])
Program Explanation

1. User must enter the first, second and third number.
2. All the elements are appending into a list for the ease of comparison.
3. The for loops range from 0-2 which are basically the indexes of the three elements in the list.
4. If none of the indexes are equal to each other, the element associated with the particular element in the list is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter first number:1
Enter second number:2
Enter third number:3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
 
Case 2:
Enter first number:5
Enter second number:7
Enter third number:3
5 7 3
5 3 7
7 5 3
7 3 5
3 5 7
3 7 5

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.