📌  相关文章
📜  打印数组中偶数和奇数索引元素的乘积的程序

📅  最后修改于: 2022-05-13 01:57:05.057000             🧑  作者: Mango

打印数组中偶数和奇数索引元素的乘积的程序

给定一个整数数组。任务是编写一个程序来分别查找偶数和奇数索引位置的元素的乘积。
注意:数组考虑基于 0 的索引。即数组中第一个元素的索引为零。
例子

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 

遍历数组,保持两个变量的偶数奇数分别存储元素和偶数和奇数索引的乘积。遍历时检查当前索引是偶数还是奇数,即 (i%2) 是否为零。如果将当前元素偶数乘以偶数索引乘积,否则将其乘以奇数索引乘积。
下面是上述方法的实现:

C++
// CPP program to find product of elements
// at even and odd index positions separately
#include 
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;
}


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


Javascript


输出:
Even Index Product : 15
Odd Index Product : 48

时间复杂度: O(n)