Put Even and Odd Numbers of an Array in two Arrays - Wisdom Lighter

Tuesday, 22 May 2018

Put Even and Odd Numbers of an Array in two Arrays

C++

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

 Output

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

No comments:
Write comments