Make a program that will transfer n odd element of an array in one array and even element in another array

This is simple c or c++ program. In this program two type of strategies is used, First one is use of array in c programming language and second one is the even and odd number test.
With the help of this simple program we can understand the working of arrays and test of even and odd numbers.
If we want to test number even or odd then simply divide number by 2 and find out remainder.
If the remainder is equal to zero then the number is even number other wise the number is odd number.Just like
5/2 remainder is 1. i.e. 5 is odd number.
and 4/2 remainder is 0 i.e. 4 is even number.

If we want to test even of odd in array then use for loop and test each element and if the number is even then put element into even number array and if the number is odd then put it into odd element array.

here the code of given problem is as.

/*By bharat kumar dhakerwww.computersciencearticle.in*
/#include<iostream.h>
#include <stdio.h>
#include <conio.h>
void main( )
{int givenArray[100], evenArray[50], oddArray[50];
int numberOfElement,bcount=0,ccount=0;

cout<<"enter number of elementsn";
cin>>numberOfElement;
for(int i = 0; i < numberOfElement; i  )
{  
cout<<"n Enter elements for "<< i 1<< " location in given array:";
cin >> givenArray[i]; 
}
for(i=0;i<numberOfElement;i  )
{
if(givenArray[i] % 2 == 0)
//test for even numbers
{evenArray[bcount]=givenArray[i];bcount  ;
}else 
//odd elements
{oddArray[ccount]=givenArray[i];ccount;
}
}
printf("nOdd elements array is:n");
for(i=0;i<ccount;i  )printf(" %d",oddArray[i]);

printf("nEven elements array is:n");for(i=0;i<bcount;i  )
printf(" %d",evenArray[i]);getch( );}

Output:
Let’s take 5 element array.
Elements is 1, 2, 3, 4, 5

1 is odd number
2 is even
3 is odd
4 is even
5 is odd

that is the
even element array is
2, 4
odd element array is
1, 3, 5

Leave a Reply

Your email address will not be published. Required fields are marked *