Python Loops Problem
Write a Python program with “for” loops to generate a list of all the pairs of positive two digit numbers whose sum is 80, and whose difference is 14.
HINTS
- Iterate through all possible combinations of two-digit numbers.
- For each pair of numbers, it checks, the sum must be 80 and their absolute difference, 14.
- Add the pairs to a list If the conditions are met.
- Finally, print the pairs of positive two-digit numbers that satisfy the given conditions.
SOLUTION
# Create a list to store the possible numbers
pairs = []
# Use nested for loops to iterate through all possible combinations of two-digit numbers
for num1 in range(10, 100): # Iterate through all two-digit numbers
for num2 in range(10, 100): # Iterate through all two-digit numbers again
if num1 + num2 == 80 and abs(num1 - num2) == 14: # Check if the sum is 60
pairs.append((num1, num2)) # If the conditions are met, add the pair to the list
print("Pairs of positive two-digit numbers whose sum is 80 and difference is 14:")
for pair in pairs:
print(pair)