Computer >> Computer tutorials >  >> Programming >> C programming

How to separate even and odd numbers in an array by using for loop in C language?


An array is a group of related data items that are stored with single name.

For example, int student[30]; //student is an array name that holds 30 collection of data items with a single variable name

Operations of array

  • Searching − It is used to find whether particular element is present or not

  • Sorting − It helps in arranging the elements in an array either in ascending or descending order.

  • Traversing − It processes every element in an array sequentially.

  • Inserting − It helps in inserting the elements in an array.

  • Deleting − It helps in deleting an element in an array.

The logic to find even numbers in an array is as follows −

for(i = 0; i < size; i ++){
   if(a[i] % 2 == 0){
      even[Ecount] = a[i];
      Ecount++;
   }
}

The logic to find odd numbers in an array is as follows −

for(i = 0; i < size; i ++){
   if(a[i] % 2 != 0){
      odd[Ocount] = a[i];
      Ocount++;
   }
}

To display even numbers, call display function as mentioned below −

printf("no: of elements comes under even are = %d \n", Ecount);
printf("The elements that are present in an even array is: ");
void display(int a[], int size){
   int i;
   for(i = 0; i < size; i++){
      printf("%d \t ", a[i]);
   }
   printf("\n");
}

To display odd numbers, call display function as given below −

printf("no: of elements comes under odd are = %d \n", Ocount);
printf("The elements that are present in an odd array is : ");
void display(int a[], int size){
   int i;
   for(i = 0; i < size; i++){
      printf("%d \t ", a[i]);
   }
   printf("\n");
}

Program

Following is the C program to separate even and odd numbers in an array by using for loop −

#include<stdio.h>
void display(int a[], int size);
int main(){
   int size, i, a[10], even[20], odd[20];
   int Ecount = 0, Ocount = 0;
   printf("enter size of array :\n");
   scanf("%d", &size);
   printf("enter array elements:\n");
   for(i = 0; i < size; i++){
      scanf("%d", &a[i]);
   }
   for(i = 0; i < size; i ++){
      if(a[i] % 2 == 0){
         even[Ecount] = a[i];
         Ecount++;
      }
      else{
         odd[Ocount] = a[i];
         Ocount++;
      }
   }
   printf("no: of elements comes under even are = %d \n", Ecount);
   printf("The elements that are present in an even array is: ");
   display(even, Ecount);
   printf("no: of elements comes under odd are = %d \n", Ocount);
   printf("The elements that are present in an odd array is : ");
   display(odd, Ocount);
   return 0;
}
void display(int a[], int size){
   int i;
   for(i = 0; i < size; i++){
      printf("%d \t ", a[i]);
   }
   printf("\n");
}

Output

When the above program is executed, it produces the following result −

enter size of array:
5
enter array elements:
23
45
67
12
34
no: of elements comes under even are = 2
The elements that are present in an even array is: 12 34
no: of elements comes under odd are = 3
The elements that are present in an odd array is : 23 45 67