Code For Alls..!

get the code!!

Wednesday, 26 July 2017

Array Element Adjacent Sum

Given an Array of length N, the program must print the sum of adjacent numbers of elements present in the array.
Input Format:
First line contains N.
Second line contains N element values seperated by a space.

Output Format:
Adjacent sum of N elements seperated by a space.
Boundary Conditions:
2 <= N <= 100

Example Input/Output 1:
Input:
5
1 1 1 1 1

Output:
1 2 2 2 1
Example Input/Output 2:
Input:
10
7 9 3 7 9 1 10 2 9 7
Output:
9 10 16 12 8 19 3 19 9 9
Example Input/Output 3:
Input:
2
1 2
Output:
2 1
C code:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("%d ",a[1]);
for(int i=1;i<n-1;i++)
printf("%d ",a[i-1]+a[i+1]);
printf("%d ",a[n-2]);
}