Given a tuple pairs=((1,1),(2,2),(9,17),(12,8)). Write a program to count the number of pairs (a,b) such that both a and b are odd
Answers
Answered by
2
Solution:
The given problem is solved in Python.
pairs = ((1, 1), (2, 2), (9, 17), (12, 8))
count = 0
for i, j in pairs:
if i % 2 != 0 and j % 2 != 0:
count += 1
print('Total pairs such that both are odd =', count)
Explanation:
- Line 1: A tuple of random pairs is created.
- Line 2: Count value is initialised with 0. This variable counts the total number of pairs present.
- Line 3: We have used multiple variables (i and j) to access each pair from the tuple.
- Line 4: Now we have checked whether both the numbers are odd.
- Line 5: If the condition be comes true, the count value is incremented.
- Line 6: Total pairs counted is displayed on the screen.
Execution:
At last, count becomes 2. So, the output is 2.
See attachment for output.
Attachments:
Similar questions