Thursday 5 July 2012

program to interchange odd and even element within array in c++.

#include<iostream>
using namespace std;

int main()
{
int n,i,j,k,temp;

    cout<<"Enter your total number here";
    cin>>n;
   
    int a[n];
    cout<<endl<<"enter element for first array"<<endl;

        for(j=0;j<n;j++)
        {
        cout<<"Enter  element at position  " <<j+1<<"  :";
        cin>>a[j];
        cout<<endl;
        }

        cout<<"before Exchange"<<endl;
        for(j=0;j<n;j++)
        {
        cout<<"Enter  element at position  " <<j+1<<"  :";
        cout<<a[j];
        cout<<endl;
        }

        for(j=1;j<=n;j++)
        {
            if(j%2==0)
            {
            temp=a[j-1];
            a[j-1]=a[j-2];
            a[j-2]=temp;
            }
        }
        cout<<"after Exchange"<<endl;
        for(j=0;j<n;j++)
        {
        cout<<"Enter  element at position  " <<j+1<<"  :";
        cout<<a[j];
        cout<<endl;
        }
    return 0;
    }
       


No comments:

Post a Comment