Friday, 9 January 2015

C Program to print the alternate elements in an array

This is a C Program which prints out the alternate elements in an array.

Here the logic is very simple,we initially read the whole array elements and prints the alternate elements by using a simple loop
for(i=0;i<n;i=i+2)


PROGRAM : 
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,n;
    printf("How many numbers? \t");
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
    {
        printf("Enter number %d : \t",i+1);
        scanf("%d",&a[i]);
    }
    printf("\nOriginal array is :\t");
    for(i=0;i<n;i++)
        printf("%d \t",a[i]);

    printf("\n\nAlternate elements :\t");
    for(i=0;i<n;i=i+2)
        printf("%d \t",a[i]);

    return 0;
}

OUTPUT :
C - Printing alternate elements in an array