Find elements

Cpp program to separate Even and Odd number from an array

Cpp program to separate Even or Odd number from an array

In this tutorial, we will discuss the concept of Cpp program to separate Even and Odd number from an array

In this post, we are going to learn how to separate the number of Odd and Even from an array and display it.

Cpp program to separate Even and Odd number

First, we must understand how to identify odd or even number

What is Even or Odd

When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10

and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers.

In my previous post, I have explained the various approaches to check whether the  number is even or odd using C++ language

 

There are three examples given below

The common procedure of these programs

  • initially, the user will enter the size of the array and store in the variable size to define the array size.
  • And declare an integer type array for stored elements.
  • Then the user enters all the elements of the array according to the array size.
  • After that, use a  loop for iterating elements of the array.
  • finally, the following two for loops(for, while, do-while) separate even and odd number using modular operator from the array.

 

Program 1

Separate even and odd numbers using for loop

The program allows the user to enter the size of the array and receives the elements from the user.

Then, it will separate the even and odd numbers from this array using for loop in C++

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[100],i,num;
    cout<<"Enter the size of the array you want\n";

    cin>>num;//reads input from user for array size
    cout<<"Enter the elements of the array\n";

    for(i=0; i<num; i++){
        cin>>arr[i];//reads input from user for array elements
    }

    cout<<"\nEven numbers of the array are \n";
     for(i=0; i<num; i++){
        if(arr[i]%2==0){//Separate even  numbers
            cout<<arr[i]<<"\t";
        }
    }

    cout<<"\nOdd numbers of the array are \n";
     for(i=0; i<=num; i++){
        if (arr[i]%2==1){//Separate odd numbers
            cout<<arr[i]<<"\t";
        }
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter the size of the array you want
8
Enter the elements of the array
56
67
87
54
35
68
80
76

Even numbers of the array are
56    54    68     80     76
Odd numbers of the array are
67    87    35

 

Program 2

Separate even and odd numbers using while loop

The program allows the user to enter the size of the array and receives the elements from the user.

Then, it will separate the even and odd numbers from this array using while loop in C++

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[100],i,num;
    cout<<"Enter size of the array you want\n";

    cin>>num;//reads input from user for array size
     cout<<"Enter the elements of the array\n";

    i=0;
    while(i<num){
        cin>>arr[i];//reads input from user for array elements
         i++;
    }

     cout<<"\nEven numbers of the array are \n";
    i=0;
     while(i<num){
        if(arr[i]%2==0){
             cout<<arr[i]<<"\t";
        }
        i++;
    }

     cout<<"\nOdd numbers of the array are \n";
    i=0;
     while(i<=num){
        if (arr[i]%2==1){
            cout<<arr[i]<<"\t";
        }
        i++;
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter size of the array you want
8
Enter the elements of the array
67
76
56
65
45
54
34
43

Even numbers of the array are
76     56      54      34
Odd numbers of the array
67     65      45       43

 

Program 3

Separate even and odd numbers using do while loop

The program allows the user to enter the size of the array and receives the elements from the user.

Then, it will separate the even and odd numbers from this array using the do-while loop in C++ language

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[100],i,num;
    cout<<"Enter size of the array you want\n";

    cin>>num;//reads input from user for array size
    cout<<"Enter the elements of the array\n";

    i=0;
    do{
        cin>>arr[i];//reads input from user for array elements
         i++;
    }while(i<num);

    cout<<"\nEven numbers of the array are \n";
    i=0;
     do{
        if(arr[i]%2==0){
            cout<<arr[i]<<"\t";
        }
        i++;
    }while(i<num);

    cout<<"\nOdd numbers of the array are \n";
    i=0;
     do{
        if (arr[i]%2==1){
            cout<<arr[i]<<"\t";
        }
        i++;
    }while(i<=num);
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter size of the array you want
6
Enter the elements of the array
46
70
97
74
26
53

Even numbers of te array are
46    70     74      26
Odd numbers of the array are
77     53

 

Similar post

Java program to Separate odd and even number from an array

C program to Separate odd and even number from an array

Java code to compute even and odd numbers of an array

C code to compute even and odd numbers of an array

Display even and odd numbers without if statement in C++

Display even and odd numbers without if statement in Java

Display even and odd numbers without if statement in C

C program to find the largest among three numbers using function

Python code to find the largest of three numbers using the function

C++ code to find the largest of three numbers using the function

Java code to find the largest of three numbers

C code to find the largest of three numbers

C++ code to find the largest of three numbers

Python code to find the largest of three numbers

Java program to find the middle of three number using Method

C code to find the middle of three number using the function

C++ code to find the middle of three number using the function

Python code to find the middle of three number using the function

Java code to find middle of three numbers

C code to find middle of three numbers

C++ code to find middle of three numbers

Python code to find middle of three numbers

Java code to find largest of three numbers in an array

Java code to find smallest of three numbers in an array

C code to find the middle of three number using the function

C++ code to find the middle of three number using the function

 

Suggested for you

The operator in C++ language

If statement in C++ language

Nested if statement in C++ language

Data type in C++ language

Variable in C++ language

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

 

Operator in C language

If statement in C language

Data type in C language

Variable in C language

 

The operator in Python language

If statement in Python language

function in Python

Data types and Variable in Python

for loop in Python language

while loop in Python language

 

Datatype and variables in Java programming language

Operator in Java language

If statements in Java language

Method in Java language

 

 

C program to separate Odd and Even numbers from an array
C program to count even and odd numbers in an array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

3 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago