C Program to Separate Odd and Even Numbers from an Array

«
»

This is a C Program to put even & odd elements of an array in 2 separate arrays.

Problem Description

The program first finds the odd and even elements of the array. Then the odd elements of an array is stored in one array and even elements of an array is stored in another array.

Problem Solution

1. Create three integer array. First to store all the elements. Second to store even elements of first array. Third to store odd elements of first array.
2. Take size of the array as input from users and define the first array by inserting elements of the array.
3. Using for loop, scan each and every element of the first array, check whether they are even or odd by taking modulo of 2 on those numbers.
4. Even numbers will get stored(copied) in second array, while odd numbers will get stored in third array.
5. Print both arrays.

Program/Source Code

Here is source code of the C program to put even & odd elements of an array in 2 separate arrays. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.  
  2.     /*
  3.      * C Program to accept N integer number and store them in an array AR.
  4.      * The odd elements in the AR are copied into OAR and other elements
  5.      * are copied into EAR. Display the contents of OAR and EAR.
  6.     */
  7.  
  8.     #include <stdio.h>
  9.     void main()
  10.     {
  11.  
  12.         long int ARR[10], OAR[10], EAR[10];
  13.         int i, j = 0, k = 0, n;
  14.  
  15.         printf("Enter the size of array AR n");
  16.         scanf("%d", &n);
  17.  
  18.         printf("Enter the elements of the array n");
  19.         for (i = 0; i < n; i++)
  20.         {
  21.             scanf("%ld", &ARR[i]);
  22.             fflush(stdin);
  23.         }
  24.  
  25.         /*  Copy odd and even elements into their respective arrays */
  26.  
  27.         for (i = 0; i < n; i++)
  28.         {
  29.             if (ARR[i] % 2 == 0)
  30.             {
  31.                 EAR[j] = ARR[i];
  32.                 j++;
  33.             }
  34.             else
  35.             {
  36.                 OAR[k] = ARR[i];
  37.                 k++;
  38.             }
  39.         }
  40.  
  41.         printf("The elements of OAR are n");
  42.         for (i = 0; i < k; i++)
  43.         {
  44.             printf("%ldn", OAR[i]);
  45.         }
  46.  
  47.         printf("The elements of EAR are n");
  48.         for (i = 0; i < j; i++)
  49.         {
  50.             printf("%ldn", EAR[i]);
  51.         }
  52.  
  53.     }
Program Explanation

1. Create three integer arrays namely ARR, OAR, EAR of some fixed capacity, 10.
2. Take size of the array as input from users and define the first array, ARR by inserting elements of the array.
3. Using for loop, scan each and every element of the first array, check whether they are even or odd by taking modulo of 2 on those numbers.
4. If the array element modulo 2, returns 0, this means the number is divisible by 2 and it is an even number. Add this number to second array.
5. If the array element modulo 2, returns 1, this means the number is not divisible by 2 and it is an odd number. Add this number to third array.
6. Repeat steps 4 and 5 until all the elements of first array are checked.
7. At last, we will be having arrays second third holding all the even and odd elements of first array respectively.
8. Print both the OAR AND EAR.
9. Exit.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Runtime Test Cases
Enter the size of array AR
6
Enter the elements of the array
34
56
78
90
12
39
The elements of OAR are
39
The elements of EAR are
34
56
78
90
12

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.