Tuesday 13 January 2015

Cpp Program to print alternate elements in an array.

This is a Cpp Program to print the alternate elements in an array.

Here we read the whole array and then printing the alternate elements in the array by incrementing the position in the array by 2.
for(i=0;i<n;i=i+2)

PROGRAM : 
#include <iostream>

using namespace std;

int main()
{
    int i,n;

    cout << "\nEnter the no of Elements: \t";
    cin >> n;
    int arr[n];
    for(i=0;i<n;i++)
    {
        cout << "\nEnter Element " <<  i+1 << ": \t";
        cin >> arr[i];
    }
     cout << "\nOriginal array is :\t";
    for(i=0;i<n;i++)
        cout << arr[i] << "\t";

    cout << "\n\nAlternate elements array is :\t";
    for(i=0;i<n;i=i+2)
        cout << arr[i] << "\t";
    return 0;
}

OUTPUT : 
Cpp - Printing alternate elements in an array