Saturday 19 March 2016

Sum of 2 arrays


Write a program to find the sum of the corresponding elements in 2 arrays.

Input Format: 

Input consists of 2n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the first array. The last 'n' integers correspond to the elements in the second array. Assume that the maximum value of n is 15.

Output Format: 
Refer sample output for details.

Sample Input 1: 
 5
 2
 3
 6
 8
 1
 1
 1
 1
 1
 1

Sample Output 1: 
 3
 4
 7
 9
 2

Code:
  #include<stdio.h>
int main(){
  int n,a[20],b[20],i,j;
  scanf("%d",&n);
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  for(j=0;j<n;j++)
    scanf("%d",&b[j]);
  for(i=0;i<n;i++)
    printf("%d ",a[i]+b[i]);
  return 0;
}

4 comments:

  1. #include
    int main()
    {
    int n,a[20],b[20],i,sum[20];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
    scanf("%d",&b[i]);
    }
    for(i=0;i<n;i++)
    {
    sum[i]=a[i]+b[i];
    printf("%d ",sum[i]);
    }
    return 0;
    }

    ReplyDelete