Home > Backend Development > C++ > body text

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

PHPz
Release: 2023-08-25 15:09:05
forward
2072 people have browsed it

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++;
   }
}
Copy after login

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++;
   }
}
Copy after login

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>");
}
Copy after login

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>");
}
Copy after login

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>");
}
Copy after login

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
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!