Home  >  Article  >  Backend Development  >  How to separate even and odd numbers in an array using for loop in C language?

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

PHPz
PHPzforward
2023-08-25 15:09:052093browse

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

An array is a group of related data items stored under a single name.

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

Array operations

  • Search - Used to find if a particular element is present

  • Sort - It helps in arranging the elements in an array in ascending or descending order arrangement.

  • Traversal - It processes each element in the array sequentially.

  • Insertion - It helps to insert elements in an array.

  • Delete - It helps in deleting elements from the array. elements in the array.

The logic of finding even numbers in the array is as follows-

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

The logic of finding odd numbers in the array is as follows-

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

To display even numbers, please call the display function mentioned below -

printf("no: of elements comes under even are = %d </p><p>", 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("</p><p>");
}

To display odd numbers, please call the display function as follows −

printf("no: of elements comes under odd are = %d </p><p>", 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("</p><p>");
}

Program

The following is a C program that uses for loop to separate even numbers and odd numbers in an array-

Live demonstration

#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 :</p><p>");
   scanf("%d", &size);
   printf("enter array elements:</p><p>");
   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 </p><p>", Ecount);
   printf("The elements that are present in an even array is: ");
   display(even, Ecount);
   printf("no: of elements comes under odd are = %d </p><p>", 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("</p><p>");
}

Output

When the above program is executed, the following results will be produced-

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

The above is the detailed content of How to separate even and odd numbers in an array using for loop in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete