Product of all pairwise consecutive elements in an Array - GeeksforGeeks

23 Apr, 2021
Given an array of integers of N elements. The task is to print the product of all of the pairwise consecutive elements.Pairwise consecutive pairs of an array of size N are (a[i], a[i+1]) for all ranging from 0 to N-2Examples:  
Input  : arr[] = {8, 5, 4, 3, 15, 20}
Output : 40, 20, 12, 45, 300

Input  : arr[] = {5, 10, 15, 20}
Output : 50, 150, 300
 
The solution is to traverse the array and calculate and print the product of every pair (arr[i], arr[i+1]).Below is the implementation of this approach:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to print the// product of the consecutive elements.#include <iostream>using namespace std; // Function to print pairwise// consecutive productvoid pairwiseProduct(int arr[], int n){    int prod = 1;    for (int i = 0; i < n - 1; i++) {         // multiply the alternate numbers        prod = arr[i] * arr[i + 1];        printf(" %d ", prod);    }} // Driver Codeint main(){    int arr[] = { 4, 10, 15, 5, 6 };    int n = sizeof(arr) / sizeof(arr[0]);     pairwiseProduct(arr, n);     return 0;}
// Java program to print the product// of the consecutive elements.import java .io.*; class GFG{// Function to print pairwise// consecutive productstatic void pairwiseProduct(int[] arr,                            int n){    int prod = 1;    for (int i = 0; i < n - 1; i++)    {         // multiply the alternate numbers        prod = arr[i] * arr[i + 1];        System.out.print(prod + " ");    }} // Driver Codepublic static void main(String[] args){    int[] arr = { 4, 10, 15, 5, 6 };    int n = arr.length;     pairwiseProduct(arr, n);}} // This code is contributed// by anuj_67..
# Python 3 program to print the# product of the consecutive elements. # Function to print pairwise# consecutive productdef pairwiseProduct( arr, n):     prod = 1    for i in range(n - 1) :         # multiply the alternate numbers        prod = arr[i] * arr[i + 1]        print(prod, end = " ")     # Driver Codeif __name__=="__main__":     arr = [ 4, 10, 15, 5, 6 ]    n = len(arr)     pairwiseProduct(arr, n) # This code is contributed# by ChitraNayal
// C# program to print the product// of the consecutive elements.using System; class GFG{// Function to print pairwise// consecutive productstatic void pairwiseProduct(int[] arr,                            int n){    int prod = 1;    for (int i = 0; i < n - 1; i++)    {         // multiply the alternate numbers        prod = arr[i] * arr[i + 1];        Console.Write(prod + " ");    }} // Driver Codepublic static void Main(){    int[] arr = { 4, 10, 15, 5, 6 };    int n = arr.Length;     pairwiseProduct(arr, n);}} // This code is contributed// by Akanksha Rai(Abby_akku)
<?php// PHP program to print the// product of the consecutive elements. // Function to print pairwise// consecutive productfunction pairwiseProduct(&$arr, $n){    $prod = 1;    for ($i = 0; $i < $n - 1; $i++)    {         // multiply the alternate numbers        $prod = $arr[$i] * $arr[$i + 1];        echo ($prod);        echo (" ");    }} // Driver Code$arr = array(4, 10, 15, 5, 6 );$n = sizeof($arr); pairwiseProduct($arr, $n); // This code is contributed// by Shivi_Aggarwal?>
<script> // Javascript program to print the// product of the consecutive elements. // Function to print pairwise// consecutive productfunction pairwiseProduct( arr,  n){    let prod = 1;    for (let i = 0; i < n - 1; i++) {         // multiply the alternate numbers        prod = arr[i] * arr[i + 1];        document.write(prod + " ");;    }}     // driver code    let arr = [ 4, 10, 15, 5, 6 ];    let n = arr.length;    pairwiseProduct(arr, n);        // This code is contributed by jana_sayantan.</script>
40  150  75  30
 
Time complexity : O(n) 
Shivi_Aggarwal
Akanksha_Rai
vt_m
ukasp
VishalBachchas
jana_sayantan
Technical Scripter 2018
Arrays
School Programming
Technical Scripter
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Window Sliding Technique
Program to find sum of elements in a given array
Find duplicates in O(n) time and O(1) extra space | Set 1
Reversal algorithm for array rotation
Next Greater Element
Python Dictionary
Inheritance in C++
Reverse a string in Java
Interfaces in Java
C++ Classes and Objects