Python Program to Accept Three Digits and Print all Possible Combinations from the Digits

PythonServer Side ProgrammingProgramming

When it is required to print all possible combination of digits when the input is taken from the user, nested loop is used.

Below is a demonstration of the same −

Example

 Live Demo

first_num = int(input("Enter the first number..."))
second_num = int(input("Enter the second number..."))
third_num = int(input("Enter the third number..."))
my_list = []
print("The first number is ")
print(first_num)
print("The second number is ")
print(second_num)
print("The third number is ")
print(third_num)

my_list.append(first_num)
my_list.append(second_num)
my_list.append(third_num)

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(my_list[i],my_list[j],my_list[k])

Output

Enter the first number...3
Enter the second number...5
Enter the third number...8
The first number is
3
The second number is
5
The third number is
8
3 5 8
3 8 5
5 3 8
5 8 3
8 3 5
8 5 3

Explanation

  • The three numbers re taken as input from the user.

  • An empty list is created.

  • The three numbers are displayed on the console.

  • These numbers are appended to the empty list.

  • Three nested loops are used, and the numbers are iterated over.

  • When they are not equal, their combinations are displayed as output on the console.

raja
Published on 16-Apr-2021 12:03:02
Advertisements