Given a number N, the task is to find the number of pairs containing an even and an odd number from numbers between 1 and N inclusive.

**Note:**Order of numbers in the pair does not matter that is (1, 2) and (2, 1) are the same.

**Examples**:

Input: N = 3 Output: 2 The pairs are (1, 2) and (2, 3). Input: N = 6 Output: 9 The pairs are (1, 2), (1, 4), (1, 6), (2, 3), (2, 5), (3, 4), (3, 6), (4, 5), (5, 6).

**Approach:** Number of ways to form the pairs is **(Total number of Even numbers*Total number of Odd numbers)**.

Thus

- if N is even number of even numbers = number of odd numbers = N/2
- if N is odd number of even numbers = N/2 and number of odd numbers = N/2+1

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 6; ` ` ` ` ` `int` `Even = N / 2 ; ` ` ` ` ` `int` `Odd = N - Even ; ` ` ` ` ` `cout << Even * Odd ; ` ` ` ` ` `return` `0; ` ` ` `// This code is contributed ` ` ` `// by ANKITRAI1 ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` `class` `GFG{ ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `N = ` `6` `; ` ` ` ` ` `int` `Even = N / ` `2` `; ` ` ` ` ` `int` `Odd = N - Even ; ` ` ` ` ` `System.out.println( Even * Odd ); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the above approach ` `N ` `=` `6` ` ` ` ` `# number of even numbers ` `Even ` `=` `N` `/` `/` `2` ` ` `# number of odd numbers ` `Odd ` `=` `N` `-` `Even ` `print` `(Even ` `*` `Odd) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the ` `// above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 6; ` ` ` ` ` `int` `Even = N / 2 ; ` ` ` ` ` `int` `Odd = N - Even ; ` ` ` ` ` `Console.WriteLine(Even * Odd); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the ` `// above approach ` ` ` `// Driver code ` `$N` `= 6; ` ` ` `$Even` `= ` `$N` `/ 2 ; ` ` ` `$Odd` `= ` `$N` `- ` `$Even` `; ` ` ` `echo` `$Even` `* ` `$Odd` `; ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

9

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count numbers from given range having odd digits at odd places and even digits at even places
- Ways to choose balls such that at least one ball is chosen
- Find if it is possible to choose subarray that it contains exactly K even integers
- Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element
- Count ways to make sum of odd and even indexed elements equal by removing an array element
- Count ways to express even number ‘n’ as sum of even integers
- Find a number containing N - 1 set bits at even positions from the right
- Count pairs in an array containing at least one even value
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Choose n elements such that their mean is maximum
- Choose points from two ranges such that no point lies in both the ranges
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Puzzle | Choose the game of Rolling Dice
- Choose X such that (A xor X) + (B xor X) is minimized
- Pair formation such that maximum pair sum is minimized
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.