Given an array of elements. The task is to print the modulus of all of the pairwise consecutive elements. That is for all pair of consecutive elements say ((a[i], a[i+1])), print **(a[i] % a[i+1])**.

**Note**: Consecutive pairs of an array of size N are (a[i], a[i+1]) for all i ranging from 0 to N-2.

**Examples**:

Input: arr[] = {8, 5, 4, 3, 15, 20}Output: 3 1 1 3 15Input: arr[] = {5, 10, 15, 20}Output: 5 10 15

**Approach: ** The solution is to traverse the array and calculate and print the modulus of every pair (arr[i], arr[i+1]).

Below is the implementation of the above approach:

## C++

`// C++ program to print the modulus ` `// of the consecutive elements ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to print pairwise modulus ` `// of consecutive elements ` `void` `pairwiseModulus(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` ` ` `// Modulus of consecutive numbers ` ` ` `cout << (arr[i] % arr[i + 1]) << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 8, 5, 4, 3, 15, 20 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `pairwiseModulus(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print the modulus ` `// of the consecutive elements ` `import` `java.util.*; ` ` ` `class` `Geeks { ` ` ` `// Function to print pairwise modulus ` `// of consecutive elements ` `static` `void` `pairwiseModulus(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `1` `; i++) { ` ` ` ` ` `// Modulus of consecutive numbers ` ` ` `System.out.println((arr[i] % arr[i + ` `1` `])); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `arr[] = { ` `8` `, ` `5` `, ` `4` `, ` `3` `, ` `15` `, ` `20` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `pairwiseModulus(arr, n); ` `} ` `} ` ` ` `// This code is contributed by ankita_saini ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to print the modulus ` `# of the consecutive elements ` ` ` `# Function to print pairwise modulus ` `# of consecutive elements ` `def` `pairwiseModulus(arr, n): ` ` ` `for` `i ` `in` `range` `(` `0` `, n ` `-` `1` `, ` `1` `): ` ` ` ` ` `# Modulus of consecutive numbers ` ` ` `print` `((arr[i] ` `%` `arr[i ` `+` `1` `]), ` ` ` `end ` `=` `" "` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `8` `, ` `5` `, ` `4` `, ` `3` `, ` `15` `, ` `20` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `pairwiseModulus(arr, n) ` ` ` `# This code is contributed ` `# by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print the modulus ` `// of the consecutive elements ` `using` `System; ` ` ` `class` `Geeks { ` ` ` `// Function to print pairwise modulus ` `// of consecutive elements ` `static` `void` `pairwiseModulus(` `int` `[] arr, ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` ` ` `// Modulus of consecutive numbers ` ` ` `Console.WriteLine((arr[i] % arr[i + 1])); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `[] arr = {8, 5, 4, 3, 15, 20}; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `pairwiseModulus(arr, n); ` `} ` `} ` ` ` `// This code is contributed by ankita_saini ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `//PHP program to print the modulus ` `// of the consecutive elements ` ` ` `// Function to print pairwise modulus ` `// of consecutive elements ` `function` `pairwiseModulus( ` `$arr` `, ` `$n` `) ` `{ ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `- 1; ` `$i` `++) { ` ` ` ` ` `// Modulus of consecutive numbers ` ` ` `echo` `(` `$arr` `[` `$i` `] % ` `$arr` `[` `$i` `+ 1]), ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` ` ` `$arr` `= ` `array` `( 8, 5, 4, 3, 15, 20 ); ` ` ` `$n` `= sizeof(` `$arr` `) / sizeof(` `$arr` `[0]); ` ` ` ` ` `pairwiseModulus(` `$arr` `, ` `$n` `); ` ` ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3 1 1 3 15

**Time complexity :** O(n)

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:

- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Product of all pairwise consecutive elements in an Array
- Absolute Difference of all pairwise consecutive elements in an array
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Check if Queue Elements are pairwise consecutive | Set-2
- Check if all array elements are pairwise co-prime or not
- Finding 'k' such that its modulus with each array element is same
- Maximize the sum of modulus with every Array element
- Modulus of two float or double numbers
- Count array elements that can be represented as sum of at least two consecutive array elements
- Check if the elements of stack are pairwise sorted
- XOR of all possible pairwise sum from two given Arrays
- Maximum sum of pairwise product in an array with negative allowed
- Check if a given array is pairwise sorted or not
- Find elements of array using XOR of consecutive elements
- Replace array elements by sum of next two consecutive elements
- Maximum number of consecutive 1's in binary representation of all the array elements
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Construct an array from GCDs of consecutive elements in given array
- Count of Array elements greater than all elements on its left and at least K elements on its right

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.