Open In App

Program to print product of even and odd indexed elements in an Array

Given an array of integers. The task is to write a program to find the product of elements at even and odd index positions separately.

Note: 0-based indexing is considered for the array. That is the index of the first element in the array is zero.

Examples

Input : arr = {1, 2, 3, 4, 5, 6}
Output : Even Index Product : 15
         Odd Index Product : 48
Explanation: Here, N = 6 so there will be 3 even 
index positions and 3 odd index positions in the array
Even = 1 * 3 * 5 = 15
Odd =  2 * 4 * 6 = 48

Input : arr = {10, 20, 30, 40, 50, 60, 70}
Output : Even Index Product : 105000
         Odd Index Product : 48000
Explanation: Here, n = 7 so there will be 3 odd
index positions and 4 even index positions in an array
Even = 10 * 30 * 50 * 70 = 1050000
Odd = 20 * 40 * 60 = 48000 

Traverse the array and keep two variables even and odd to store the product of elements and even and odd indexes respectively. While traversing check if the current index is even or odd, i.e. (i%2) is zero or not. If even multiply current element with even indexed product otherwise multiply it with odd indexed product.

Below is the implementation of the above approach: 




// CPP program to find product of elements
// at even and odd index positions separately
#include <iostream>
using namespace std;
 
// Function to calculate product
void EvenOddProduct(int arr[], int n)
{
    int even = 1;
    int odd = 1;
 
    for (int i = 0; i < n; i++) {
 
        // Loop to find even, odd product
        if (i % 2 == 0)
            even *= arr[i];
        else
            odd *= arr[i];
    }
 
    cout << "Even Index Product : " << even << endl;
    cout << "Odd Index Product : " << odd;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    EvenOddProduct(arr, n);
 
    return 0;
}




// C program to find product of elements
// at even and odd index positions separately
#include <stdio.h>
 
// Function to calculate product
void EvenOddProduct(int arr[], int n)
{
    int even = 1;
    int odd = 1;
 
    for (int i = 0; i < n; i++) {
 
        // Loop to find even, odd product
        if (i % 2 == 0)
            even *= arr[i];
        else
            odd *= arr[i];
    }
     
    printf("Even Index Product : %d\n",even);
    printf("Odd Index Product : %d\n",odd);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    EvenOddProduct(arr, n);
 
    return 0;
}
 
// This code is contributed by kothavvsaakash




// Java program to find product of elements
// at even and odd index positions separately
 
class GFG
{
     
    // Function to calculate product
    static void EvenOddProduct(int arr[], int n)
    {
        int even = 1;
        int odd = 1;
     
        for (int i = 0; i < n; i++) {
     
            // Loop to find even, odd product
            if (i % 2 == 0)
                even *= arr[i];
            else
                odd *= arr[i];
        }
     
        System.out.println("Even Index Product : " + even);
        System.out.println("Odd Index Product : " + odd);
    }
     
    // Driver Code
    public static void main(String []args)
    {
        int arr[] = { 1, 2, 3, 4, 5, 6 };
        int n = arr.length;
     
        EvenOddProduct(arr, n);
             
    }
 
   // This code is contributed by ihritik
}




# Python3 program to find product of elements
# at even and odd index positions separately
 
  
 
# Function to calculate product
def EvenOddProduct(arr, n):
  
    even = 1 
    odd = 1 
 
    for i in range (0,n):
 
        # Loop to find even, odd product
        if (i % 2 == 0):
            even *= arr[i] 
        else:
            odd *= arr[i] 
      
 
    print("Even Index Product : " , even) 
    print("Odd Index Product : " , odd) 
  
 
# Driver Code
  
arr =   1, 2, 3, 4, 5, 6   
n = len(arr)
 
EvenOddProduct(arr, n) 
 
# This code is contributed by ihritik




// C# program to find product of elements
// at even and odd index positions separately
 
using System;
class GFG
{
     
    // Function to calculate product
    static void EvenOddProduct(int []arr, int n)
    {
        int even = 1;
        int odd = 1;
     
        for (int i = 0; i < n; i++) {
     
            // Loop to find even, odd product
            if (i % 2 == 0)
                even *= arr[i];
            else
                odd *= arr[i];
        }
     
        Console.WriteLine("Even Index Product : " + even);
        Console.WriteLine("Odd Index Product : " + odd);
    }
     
    // Driver Code
    public static void Main()
    {
        int []arr = { 1, 2, 3, 4, 5, 6 };
        int n = arr.Length;
     
        EvenOddProduct(arr, n);
     
         
    }
 
   // This code is contributed by ihritik
}




<?php
 
// PHP program to find product of elements
// at even and odd index positions separately
 
  
 
// Function to calculate product
function EvenOddProduct($arr, $n)
{
    $even = 1;
    $odd = 1 ;
 
    for($i=0;$i<$n;$i++)
    {
        // Loop to find even, odd product
        if ($i % 2 == 0)
            $even *= $arr[$i];
        else
            $odd *= $arr[$i];
      
    }
    echo "Even Index Product: " .$even;
    echo "\n";
    echo "Odd Index Product: "  .$odd
  
}
// Driver Code
  
$arr =   array(1, 2, 3, 4, 5, 6) ;  
$n = sizeof($arr);
 
EvenOddProduct($arr, $n);
 
// This code is contributed by ihritik
 
?>




<script>
 
// Javascript program to find product of elements
// at even and odd index positions separately
 
 
// Function to calculate product
function EvenOddProduct(arr, n)
{
    let even = 1;
    let odd = 1;
 
    for (let i = 0; i < n; i++) {
 
        // Loop to find even, odd product
        if (i % 2 == 0)
            even *= arr[i];
        else
            odd *= arr[i];
    }
 
    document.write("Even Index Product : " + even + "<br>");
    document.write("Odd Index Product : " + odd);
}
 
// Driver Code
  
    let arr = [ 1, 2, 3, 4, 5, 6 ];
    let n = arr.length;
 
    EvenOddProduct(arr, n);
 
 
// This code is contributed by Mayank Tyagi
 
</script>

Output
Even Index Product : 15
Odd Index Product : 48

Time complexity: O(n)

Auxiliary space: O(1) as it is using constant space for variables

Method 2: Using bitwise & operator 




// CPP program to find product of elements
// at even and odd index positions separately using bitwise & operator
#include <iostream>
using namespace std;
 
// Function to calculate product
void EvenOddProduct(int arr[], int n)
{
    int even = 1;
    int odd = 1;
 
    for (int i = 0; i < n; i++) {
 
        // Loop to find even, odd product
        if (i & 1 != 0)
            odd *= arr[i];
        else
            even *= arr[i];
    }
 
    cout << "Even Index Product : " << even << endl;
    cout << "Odd Index Product : " << odd;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    EvenOddProduct(arr, n);
 
    return 0;
}
 
//This code is contributed by Vinay Pinjala.




// Java program to find product of elements
// at even and odd index positions separately using bitwise
// & operator
 
public class GFG {
 
    // Function to calculate product
    public static void evenOddProduct(int[] arr)
    {
        int even = 1;
        int odd = 1;
 
        for (int i = 0; i < arr.length; i++) {
 
            // Loop to find even, odd product
            if ((i & 1) != 0)
                odd *= arr[i];
            else
                even *= arr[i];
        }
 
        System.out.println("Even Index Product: " + even);
        System.out.println("Odd Index Product: " + odd);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5, 6 };
        evenOddProduct(arr);
    }
}




# Python program to find product of elements
# at even and odd index positions separately using bitwise & operator
 
# Function to calculate product
def even_odd_product(arr):
    even = 1
    odd = 1
 
    for i in range(len(arr)):
 
        # Loop to find even, odd product
        if i & 1 != 0:
            odd *= arr[i]
        else:
            even *= arr[i]
 
    print("Even Index Product:", even)
    print("Odd Index Product:", odd)
 
 
# Driver COde
arr = [1, 2, 3, 4, 5, 6]
even_odd_product(arr)




// C# program to find product of elements
// at even and odd index positions separately using bitwise
// & operator
 
using System;
 
public class GFG {
 
    // Function to calculate product
    public static void EvenOddProduct(int[] arr)
    {
        int even = 1;
        int odd = 1;
 
        for (int i = 0; i < arr.Length; i++) {
 
            // Loop to find even, odd product
            if ((i & 1) != 0)
                odd *= arr[i];
            else
                even *= arr[i];
        }
 
        Console.WriteLine("Even Index Product: " + even);
        Console.WriteLine("Odd Index Product: " + odd);
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 4, 5, 6 };
        EvenOddProduct(arr);
    }
}




// Javascript program to find product of elements
// at even and odd index positions separately using bitwise
// & operator
 
// Function to calculate product
function EvenOddProduct(arr, n) {
  let even = 1;
  let odd = 1;
 
  for (let i = 0; i < n; i++) {
    // Loop to find even, odd product
    if (i & 1 != 0)
        odd *= arr[i];
    else
        even *= arr[i];
  }
 
  console.log("Even Index Product : " + even);
  console.log("Odd Index Product : " + odd);
}
 
// Driver Code
let arr = [1, 2, 3, 4, 5, 6];
let n = arr.length;
 
EvenOddProduct(arr, n);

Output
Even Index Product : 15
Odd Index Product : 48

Time complexity: O(n)

Auxiliary space: O(1) as it is using constant space for variables


Article Tags :