This blog is under construction

Wednesday 24 July 2013

C program to separate even and odd numbers from an array

Write a C program to separate even and odd numbers from an array.


  #include <stdio.h>
  #define MAXLIMIT 256

  int main() {
        int i, n, ele[MAXLIMIT];

        /* get the number of elements from the user */
        printf("Number of elements:");
        scanf("%d", &n);

        /* get the input array elements from the user */
        for (i = 0; i < n; i++) {
                printf("Data[%d]: ", i);
                scanf("%d", &ele[i]);
        }

        /* printing the even elements */
        printf("\nEven elements in the give array:\n{");
        for (i = 0; i < n; i++) {
                if (ele[i] % 2 == 0) {
                        printf("%d, ", ele[i]);
                }
        }
        printf("\b\b}");

        /* printing the odd elements */
        printf("\nOdd elements in the given array:\n{");
        for (i = 0; i < n; i++) {
                if (ele[i] % 2 != 0) {
                        printf("%d, ", ele[i]);
                }
        }
        printf("\b\b}\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Number of elements:6
  Data[0]: 1
  Data[1]: 2
  Data[2]: 3
  Data[3]: 4
  Data[4]: 5
  Data[5]: 6

  Even elements in the give array:
  {2, 4, 6} 
  Odd elements in the given array:
  {1, 3, 5} 


No comments:

Post a Comment