Friday, June 26, 2020

Sum of odd positions and even positions in array


Sum of odd positions and even positions in an array

PROGRAM:


#include <stdio.h>
int main()
{
  int n, a[100],b[100],i,j,even=0,odd=0;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++)
  {
         
      if(i%2==0)
      {
          even=even+a[i];
      }   
      else
      {
          odd=odd+a[i];
      }
     
  }
  printf("Sum of even position is %d\n",even);
  printf("Sum of odd position is %d",odd);
}


OUTPUT:

5                                                                                                                     
1 3 5 7 8                                                                                                             
Sum of even position is 14                                                                                            
Sum of odd position is 10

No comments:

Post a Comment