- In this article we are going to discuss about How to write a program to separate even and odd numbers in an array.
- Hope this article will help you to add some extra knowledge to you to learn programming so you must read the article full to get the important points from the article.
- We solve this problem by the help of C++ programming language If You do not know about C++ Programming don't worry just understand the logic i discussed below and you will get an idea .
The Below points mention are discussed in this article
- Question
- Logic to solve
- source code
- source code explanation
- output
- feedback
Question -
write a program to separate even and odd numbers in an array.
![]() |
write a program to separate even and odd numbers in an array |
Logic to solve
Initialize two index variables left and right:
left = 0, right = size -1
2) Keep incrementing left index until we see an even number.
3) Keep decrementing right index until we see an odd number.
4) If left < right then swap arr[left] and arr[right]
Source code
#include <iostream>
#include <algorithm>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void evenodd(int arr[], int n)
{
int leftnum = 0, rightnum = n - 1;
while (leftnum < rightnum)
{
while (arr[leftnum] % 2 == 0 && leftnum < rightnum)
leftnum++;
while (arr[rightnum] % 2 == 1 && leftnum < rightnum)
rightnum--;
if (leftnum < rightnum)
{
swap(&arr[leftnum], &arr[rightnum]);
leftnum++;
rightnum--;
}
}
}
int main()
{
int n;
int arr[10];
cout << "Enter the array size :" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Enter the data no. " << i + 1 << ": ";
cin >> arr[i];
}
evenodd(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Source code explanation
- First we make a function which will swap two numbers . (we make this function because of we have to swapping between even numbers and odd numbers so we make this function ).
- Then again we make a function for finding the even and odd numbers , Inside this function we use a while loop it is checking the elements from left index to right index.
- Inside this we again run two for loops which will check for even and odd numbers .
- Our goal is to print first all even numbers the odd numbers so when we check even numbers we increment it and for odd number decrease .
- Again we use if condition then we swapping the even and odd numbers first we print the even numbers then the odd numbers.
- Then inside the main function we call the function and print out the array using for loop.
- First we make a function which will swap two numbers . (we make this function because of we have to swapping between even numbers and odd numbers so we make this function ).
- Then again we make a function for finding the even and odd numbers , Inside this function we use a while loop it is checking the elements from left index to right index.
- Inside this we again run two for loops which will check for even and odd numbers .
- Our goal is to print first all even numbers the odd numbers so when we check even numbers we increment it and for odd number decrease .
- Again we use if condition then we swapping the even and odd numbers first we print the even numbers then the odd numbers.
- Then inside the main function we call the function and print out the array using for loop.
Output
enter the range - 6
2 5 4 7 8 3
The arrays are
2 4 8 5 7 3
Check Out OUR Home page for more information about practice problem
Also check Out
Also check Out Projects for Resume
Problems On C++
Q-2) Vectors In c++
Problems on TCS paper
1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187
0 , 0 , 2 , 1 , 4 , 2 , 6 , 3 , 8 , 4 , 10 , 5 , 12 , 6 , 14 , 7 , 16 , 8
1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187
0 , 0 , 2 , 1 , 4 , 2 , 6 , 3 , 8 , 4 , 10 , 5 , 12 , 6 , 14 , 7 , 16 , 8
FEEDBACK
If still you have any doubts please comment below and please share your approaches with us .
If still you have any doubts please comment below and please share your approaches with us .
Post a Comment
Post a Comment
Please do not spam in comment