C++ program to print alternate elements from an array

      Comments Off on C++ program to print alternate elements from an array

Alternate elements

It means printing every second element from an array.Here is the picture repersentation of the alternate elements.In this picture the arrow targetting to the elements will be printed and other elements will not be printed.

alternate element

  • This program illustrate the concept of printing an alternate element from an array.We have used the very simple logic i.e. “i=i+2” it means each time the for loop will execute program will increment the value of “i” by 2.

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int x[100],i,y;
 cout<<"Enter size of array:- ";
 cin>>y;
 cout<<"Enter elements in array:- ";
 for(i=0;i<y;i++)      
  {
     cin>>x[i];
  }
cout<<"Alternate elements in array :- ";
for(i=0;i<y;i=i+2)  
 {
     cout<<"\n"<<x[i];
 }
getch();
}

OUTPUT:-

alternate elements


Comment below if you are facing any problem.


Also read

Insert an element Into sorted array
C program to enter a element into sorted array