Given an array print sum of the pairwise consecutive elements.

Examples:

Input : 8, 5, 4, 3, 15, 20 Output : 13, 9, 7, 18, 35 Input : 5, 10, 15, 20 Output : 15, 25, 35

The solution is to traverse the array and saving the sum of consecutive numbers in the variable sum.

## C++

`// C++ program to print the ` `// sum of the consecutive elements. ` `#include <stdio.h> ` `#include <stdlib.h> ` ` ` `// Function to print pairwise sum ` `void` `pairwiseSum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) ` ` ` `{ ` ` ` `// adding the alternate numbers ` ` ` `sum = arr[i] + arr[i + 1]; ` ` ` `printf` `(` `" %d "` `, sum); ` ` ` `} ` `} ` ` ` `// Driver function to test function ` `int` `main() ` `{ ` ` ` `int` `arr[] = {4, 10, 15, 5, 6}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `pairwiseSum(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print the ` `// sum of the consecutive elements. ` ` ` `class` `Arraysum { ` ` ` ` ` `// Function to print Alternatesum ` ` ` `static` `void` `pairwiseSum(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i + ` `1` `< n; i++) ` ` ` `{ ` ` ` `// adding the alternate numbers ` ` ` `sum = arr[i] + arr[i + ` `1` `]; ` ` ` `System.out.print(sum + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `/*driver function to test function*/` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `arr[] = {` `4` `, ` `10` `, ` `15` `, ` `5` `, ` `6` `}; ` ` ` `int` `n = arr.length; ` ` ` `pairwiseSum(arr, n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to print the ` `# sum of the consecutive elements. ` ` ` `# Function to print alternate sum ` `def` `pairwiseSum(lst, n): ` ` ` `sum` `=` `0` `; ` ` ` `for` `i ` `in` `range` `(` `len` `(lst)` `-` `1` `): ` ` ` ` ` `# adding the alternate numbers ` ` ` `sum` `=` `lst[i] ` `+` `lst[i ` `+` `1` `] ` ` ` `print` `(` `sum` `, end ` `=` `" "` `) ` ` ` `# driver function to test function ` `arr ` `=` `[` `4` `, ` `10` `, ` `15` `, ` `5` `, ` `6` `] ` `size ` `=` `len` `(arr) ` `pairwiseSum(arr, size) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print the ` `// sum of the consecutive elements. ` `using` `System; ` ` ` `class` `Arraysum { ` ` ` ` ` `// Function to print Alternatesum ` ` ` `static` `void` `pairwiseSum(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i + 1 < n; i++) ` ` ` `{ ` ` ` `// adding the alternate numbers ` ` ` `sum = arr[i] + arr[i + 1]; ` ` ` `Console.Write(sum + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `[]arr = {4, 10, 15, 5, 6}; ` ` ` `int` `n = arr.Length; ` ` ` `pairwiseSum(arr, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to print the ` `// sum of the consecutive elements. ` ` ` `// Function to print pairwise sum ` `function` `pairwiseSum(` `$arr` `, ` `$n` `) ` `{ ` ` ` `$sum` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `- 1; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// adding the alternate numbers ` ` ` `$sum` `= ` `$arr` `[` `$i` `] + ` `$arr` `[` `$i` `+ 1]; ` ` ` `echo` `$sum` `,` `" "` `; ` ` ` `} ` `} ` ` ` ` ` `// Driver Code ` ` ` `$arr` `= ` `array` `(4, 10, 15, 5, 6); ` ` ` `$n` `= sizeof(` `$arr` `) ; ` ` ` `pairwiseSum(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

Output:

14 25 20 11

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count array elements that can be represented as sum of at least two consecutive array elements
- Replace array elements by sum of next two consecutive elements
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Generate original array from difference between every two consecutive elements
- Rearrange an array such that product of every two consecutive elements is a multiple of 4
- Find elements of array using XOR of consecutive elements
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Rearrange an array to minimize sum of product of consecutive pair elements
- Construct an array from GCDs of consecutive elements in given array
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Check if N can be represented as sum of squares of two consecutive integers
- Find minimum sum such that one of every three consecutive elements is taken
- Minimum sum subsequence such that at least one of every four consecutive elements is picked
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Check if each element of an Array is the Sum of any two elements of another Array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Check if array elements are consecutive | Added Method 3

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.