Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #The below code snippet inputs a list of integers and
- #Find all pairs of numbers in the list whose product is even and whose sum is odd.
- #User input
- user_list = input("Enter the list of numbers in a comma seperated way. eg: 1,3,5,67,8 \n")
- # Try catch to handle the case, if the user doesn't input correctly
- try:
- num_list = [int(x.strip()) for x in user_list.split(',')]
- except:
- print("\n\nThe list of number is not entered correctly. Please run again.")
- print("The code is running on a dummy list for now [1,2,3,4,5]")
- num_list = [1,2,3,4,5]
- #Initialise lists to store results
- prod_list = []
- sum_list = []
- #Iterate over the list to find the pairs
- for i in range(len(num_list)):
- for j in range(i, len(num_list)):
- if (num_list[i] * num_list[j]) % 2 == 0:
- prod_list.append((num_list[i],num_list[j]))
- if (num_list[i] + num_list[j]) % 2 != 0:
- sum_list.append((num_list[i],num_list[j]))
- # Print the results
- print("\n\nPair of numbers having even product:",prod_list)
- print("\nPair of numbers having odd sum:",sum_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement