Print the Alternate Elements in an Array

Given an array , Print the Alternate Elements in an Array


For example:

If the input array is { 1, 2, 3, 4, 5 }


then the answer is 1 3 5 which are alternate elements of the array.


Implementation:


#include <stdio.h>
#include <stdlib.h>
int main(void) {
 int i,*a,n;
 
 /* Get the number of elements of the array */
 scanf("%d",&n);
 
 /* Allocate the memory for n integers and store the starting address in a */
 a=(int*)malloc(sizeof(int)*n);
 
 /* Get the elements of the array */
 for(i=0;i<n;i++)
  scanf("%d",(a+i));
 
 /* printing alternate elements of the array */
 for(i=0;i<n;i+=2)
  printf("%d ",*(a+i));
  
 return 0;
}