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-2
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++
// C++ program to print the
// product of the consecutive elements.
#include <iostream>
usingnamespacestd;
// Function to print pairwise
// consecutive product
voidpairwiseProduct(intarr[], intn)
{
intprod = 1;
for(inti = 0; i < n - 1; i++) {
// multiply the alternate numbers
prod = arr[i] * arr[i + 1];
printf(" %d ", prod);
}
}
// Driver Code
intmain()
{
intarr[] = { 4, 10, 15, 5, 6 };
intn = sizeof(arr) / sizeof(arr[0]);
pairwiseProduct(arr, n);
return0;
}
C
// C program to print the
// product of the consecutive elements.
#include <stdio.h>
// Function to print pairwise
// consecutive product
voidpairwiseProduct(intarr[], intn)
{
intprod = 1;
for(inti = 0; i < n - 1; i++) {
// multiply the alternate numbers
prod = arr[i] * arr[i + 1];
printf(" %d ", prod);
}
}
// Driver Code
intmain()
{
intarr[] = { 4, 10, 15, 5, 6 };
intn = sizeof(arr) / sizeof(arr[0]);
pairwiseProduct(arr, n);
return0;
}
// This code is contributed by kothavvsaakash.
Java
// Java program to print the product
// of the consecutive elements.
importjava .io.*;
classGFG
{
// Function to print pairwise
// consecutive product
staticvoidpairwiseProduct(int[] arr,
intn)
{
intprod = 1;
for(inti = 0; i < n - 1; i++)
{
// multiply the alternate numbers
prod = arr[i] * arr[i + 1];
System.out.print(prod + " ");
}
}
// Driver Code
publicstaticvoidmain(String[] args)
{
int[] arr = { 4, 10, 15, 5, 6};
intn = arr.length;
pairwiseProduct(arr, n);
}
}
// This code is contributed
// by anuj_67..
Python 3
# Python 3 program to print the
# product of the consecutive elements.
# Function to print pairwise
# consecutive product
defpairwiseProduct( arr, n):
prod =1
fori inrange(n -1) :
# multiply the alternate numbers
prod =arr[i] *arr[i +1]
print(prod, end =" ")
# Driver Code
if__name__=="__main__":
arr =[ 4, 10, 15, 5, 6]
n =len(arr)
pairwiseProduct(arr, n)
# This code is contributed
# by ChitraNayal
C#
// C# program to print the product
// of the consecutive elements.
usingSystem;
classGFG
{
// Function to print pairwise
// consecutive product
staticvoidpairwiseProduct(int[] arr,
intn)
{
intprod = 1;
for(inti = 0; i < n - 1; i++)
{
// multiply the alternate numbers
prod = arr[i] * arr[i + 1];
Console.Write(prod + " ");
}
}
// Driver Code
publicstaticvoidMain()
{
int[] arr = { 4, 10, 15, 5, 6 };
intn = arr.Length;
pairwiseProduct(arr, n);
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
PHP
<?php
// PHP program to print the
// product of the consecutive elements.
// Function to print pairwise
// consecutive product
functionpairwiseProduct(&$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
?>
Javascript
<script>
// Javascript program to print the
// product of the consecutive elements.
// Function to print pairwise
// consecutive product
functionpairwiseProduct( 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>
Output
40 150 75 30
Time Complexity: O(n), where n is the length of the given array Auxiliary Space: O(1), As constant extra space is used.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!