Here is a C++ program to print the elements at the odd and even positions in an array
SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
int a[20],num;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements of array : ";
for(int i=0;i<num;i++)
cin>>a[i];
cout<<"Elements at odd positions : ";
for(int i=0;i<num;i+=2)
cout<<a[i]<<" ";
cout<<endl<<"Elements at even positions : ";
for(int i=1;i<num;i+=2)
cout<<a[i]<<" ";
return 0;
}
using namespace std;
int main()
{
int a[20],num;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements of array : ";
for(int i=0;i<num;i++)
cin>>a[i];
cout<<"Elements at odd positions : ";
for(int i=0;i<num;i+=2)
cout<<a[i]<<" ";
cout<<endl<<"Elements at even positions : ";
for(int i=1;i<num;i+=2)
cout<<a[i]<<" ";
return 0;
}
Any questions regarding to program please write in comments.
No comments:
Post a Comment