Suma de dos elementos consecutivos en una array

Dada una suma de impresión de array de los elementos consecutivos por pares.

Ejemplos: 

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

Input  : 5, 10, 15, 20
Output : 15, 25, 35 

La solución es recorrer la array y guardar la suma de números consecutivos en la variable suma.  

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;
}

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);
    }
}

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)

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.

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
?>

Javascript

<script>
 
// Javascript program to print the
// sum of the consecutive elements.
 
// Function to print Alternatesum
function pairwiseSum(arr, n)
{
    let sum = 0;
    for(let i = 0; i + 1 < n; i++)
    {
         
        // Adding the alternate numbers
        sum = arr[i] + arr[i + 1];
        document.write(sum + " ");
    }
}
 
// Driver code
let arr = [ 4, 10, 15, 5, 6 ];
let n = arr.length;
 
pairwiseSum(arr, n);
 
// This code is contributed by divyesh072019
 
</script>

Producción: 

14 25 20 11

Publicación traducida automáticamente

Artículo escrito por pranav gupta 8 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *