Computer Science, asked by Littled58, 18 days ago

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 anindyaadhikari13
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:

  1. Line 1: A tuple of random pairs is created.
  2. Line 2: Count value is initialised with 0. This variable counts the total number of pairs present.
  3. Line 3: We have used multiple variables (i and j) to access each pair from the tuple.
  4. Line 4: Now we have checked whether both the numbers are odd.
  5. Line 5: If the condition be comes true, the count value is incremented.
  6. Line 6: Total pairs counted is displayed on the screen.

Execution:

\boxed{\begin{array}{c|c|c|c|c}\rm Iteration\: count&\rm i&\rm j&\rm Is\: both\: odd?&\rm Count\\ \rm1&\rm1&\rm1&\rm True&\rm 0+1\\ \rm2&\rm 2&\rm2&\rm False&\rm1+0\\ \rm3&\rm9&\rm 17&\rm True&\rm 1+1\\ \rm4&\rm 12&\rm 8&\rm False&\rm2+0\end{array}}

At last, count becomes 2. So, the output is 2.

See attachment for output.

Attachments:
Similar questions