10/28/2016

C++ Program For Segregate Even And Odd Numbers.

Problem :- Given An Array A[], Write A C++ Program That Segregates Even And Odd Numbers. The Functions Should Put All Odd Numbers First, And Then Even Numbers .

Solution:-

   
#include<iostream>
using namespace std;
int main()
{
int a[100];
int n,i,j,left=0,right,temp;
cout<<"enter the size of array ";
cin>>n;
cout<<"\n enter the elelment of the array \n";
for(i=0;i<n;i++)
cin>>a[i];
right=n-1;
while(left<right)
{
if(a[left]%2!=0&left<right)
left++;
if(a[right]%2==0&&left<right)
right--;
   if(a[right]%2!=0&&a[left]%2==0&&left<right)
      {
    temp=a[right];
    a[right]=a[left];
    a[left]=temp;
    left++;
   right--;
     }
}
cout<<"segregates of array :";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}

Output :-

                     
C++ Program For Segregates Even And Odd Numbers.
               
subodh yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment