Divide an array into two arrays, one containing odd integers and another containing even integers
C program to divide an array into two arrays, one containing odd integers and another containing even integers.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void read_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&a[i]);
}
}
void print_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:%d\n",i+1,a[i]);
}
}
void main()
{
int arr[MAX], odd[MAX], even[MAX];
int n, i, n_odd, n_even;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements of the array\n");
read_array(arr, n);
n_odd = 0;
n_even = 0;
for(i=0;i<n;i++)
{
if(arr[i]%2==0)
{
even[n_even] = arr[i];
n_even++;
}
else
{
odd[n_odd] = arr[i];
n_odd++;
}
}
printf("\nEven integers array\n");
print_array(even, n_even);
printf("\nOdd integers array\n");
print_array(odd, n_odd);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements of the array
Element 1 :12
Element 2 :25
Element 3 :56
Element 4 :69
Element 5 :98
Element 6 :85
Element 7 :54
Element 8 :41
Even integers array
Element 1 :12
Element 2 :56
Element 3 :98
Element 4 :54
Odd integers array
Element 1 :25
Element 2 :69
Element 3 :85
Element 4 :41