# Modulus of all pairwise consecutive elements in an Array

• Difficulty Level : Basic
• Last Updated : 17 May, 2022

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 15

Input: 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 ``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;``}`

## C

 `// C program to print the modulus``// of the consecutive elements``#include ` `// 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``        ``printf``(``"%d "``,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;``}` `// This code is contributed by kothavvsaakash.`

## 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`

## 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`

## 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`

## PHP

 ``

## Javascript

 ``
Output:
`3 1 1 3 15`

Time complexity : O(n)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up