Sunday, 20 January 2019

Sum of every X integers


The program must accept N integers and an integer X (where X is the factor of N) as the input. The program must print the sum of every X integers among N integers as the output.
Boundary Condition(s):
1 <= N <= 1000
1 <= Each integer value <= 1000
1 <= X <= N
Input Format:
The first line contains the integer N.
The second line contains N integers separated by space(s).
The third line contains the integer X.
Output Format:
The first line contains the sum of every X integers among N integers separated by a space.
Example Input/Output 1:
Input:
8
2 3 4 9 8 7 1 5
4
Output:
18 21
Explanation:
The first 4 integers are 2, 3, 4 and 9. So their sum is 18.
The next 4 integers are 8, 7, 1 and 5. So their sum is 21.
Hence the output is 18 21

Program
#include<stdio.h>
#include <stdlib.h>

int main()
{
   int N,A[1001],i,X,b=0,c=0,cnt=0;
   scanf("%d",&N);
   for(i=0;i<N;i++)
      scanf("%d",&A[i]);
   scanf("%d",&X);
   cnt=X;
   for(i=0;i<N;i++)
   {
       b+=A[i];
       --cnt;
       if (cnt==0)
       {
        printf("%d ",b);
        b=0;
        cnt=X;
       }
       
   }

}

Input/Output:
Input:
9
21 7 46 37 5 73 6 8 9
1
Output
21 7 46 37 5 73 6 8 9

No comments:

Post a Comment