Given Arr[10] = {7, 9, 13, 15, 16, 10, 12, 5, 20, 27}
Write a program to construct array ODD[] and EVEN[] from Arr[10] as you realize ODD[] consists of odd number and EVEN[] consists of even number.
for c++
Ans:-
Code:-
#include <iostream>
using namespace std;
int main() {
//intialize given array
int Arr[]= {7, 9, 13, 15, 16, 10, 12, 5, 20, 27},o=0,e=0;
// loop to find the number of even and odd number in the given array
cout<<"The given array is: ";
for(int i=0;i<10;i++)
{
cout<<Arr[i]<<" ";
if(Arr[i]%2 == 0)
e++; // if the element is even, then update the count of e
else
o++; // if the element is odd, then update the count of o
}
int ODD[o], EVEN[e]; //declare two array ODD and EVEN of size o and e respectively
//update the value of e and o to zero in order to track the index of EVEN and ODD ARRAY respectively
e=0;
o=0;
for(int i=0;i<10;i++)
{
if(Arr[i]%2 == 0) // if the element is even, then store it into EVEN array
EVEN[e++] = Arr[i];
else // if the element is odd, then store it into ODD array
ODD[o++] = Arr[i];
}
cout<<"\nEVEN ARRAY IS: ";
//Print the even ARRAY
for(int i=0;i<e;i++)
cout<<EVEN[i]<<" ";
//Print the ODD array
cout<<"\nODD ARRAY IS: ";
for(int i=0;i<o;i++)
cout<<ODD[i]<<" ";
return 0;
}
Output:-
Get Answers For Free
Most questions answered within 1 hours.