Friday, 3 July 2015

Sum of adjacent pairs

You are given a sequence of numbers, ending with a -1. You can assume that are at least two numbers before the ending -1.
Let us call the sequence x0 x1 ... xn -1.
You have to output the sequence of sums of adjacent pairs of numbers, as follows:
x0+x1 x1+x2 ... xn-1+xn
Note that the sums are separated by spaces. Kindly do not use arrays in the code.

Sample Input

 1 3 4 -1 

Sample Output

 4 7 

/*CODE*/
#include<stdio.h>
void sum(int a,int b)
{
printf("%d",a+b);
}
int main()
{
int a,b,num;
scanf("%d",&a);
if(a==-1)
return 0;
scanf("%d",&b);
if(b==-1)
return 0;
sum(a,b);
a=b;
scanf("%d",&b);
while(b!=-1)
{
printf(" ");
sum(a,b);
a=b;
scanf("%d",&b);
}
printf("\n");
return 0;
}

No comments:

Post a Comment