Program in C and C++ to store even and odd elements in different arrays from an array.
C
CPP
#include<stdio.h> #define size 25 int main() { int arr[size],even_arr[size],odd_arr[size],n,i,j,k; printf("Enter the size of array:-"); scanf("%d",&n); if(n>size) { printf("Invilid array size."); return 0; } printf("Enter %d numbers:-",n); for(i=0;i<n;i++) scanf("%d",&arr[i]); for(j=0,k=0,i=0;i<n;i++) { if(arr[i]%2==0) even_arr[j++]=arr[i]; else odd_arr[k++]=arr[i]; } printf("\nEven elements "); for(i=0;i<j;i++) printf("%2d",even_arr[i]); printf("\nOdd elements "); for(i=0;i<k;i++) printf("%2d",odd_arr[i]); return 0; }
#include<iostream> using namespace std; int main() { const int size=35; int arr[size],even_arr[size],odd_arr[size],n,i,j,k; cout<<"Enter the size of array:-"; cin>>n; if(n>size) { cout<<"Invilid array size."; return 0; } cout<<"Enter "<<n<<" numbers:-"; for(i=0;i<n;i++) cin>>arr[i]; for(j=0,k=0,i=0;i<n;i++) { if(arr[i]%2==0) even_arr[j++]=arr[i]; else odd_arr[k++]=arr[i]; } cout<<"\nEven elements "; for(i=0;i<j;i++) cout<<" "<<even_arr[i]; cout<<"\nOdd elements "; for(i=0;i<k;i++) cout<<" "<<odd_arr[i]; return 0; }
Output:-
0 Comments