Number of ways to choose a pair containing an even and an odd number from 1 to N

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

  1. if N is even number of even numbers = number of odd numbers = N/2
  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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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


Output:

9


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.