Check value

Count even and odd numbers of an array in C++

Count even and odd numbers of an array in C++

In this tutorial, we will discuss  the concept of the Count even and odd numbers of an array in C++

In this post, we are going to learn how to count even and odd numbers from the array of  given numbers in the C++ programming language

 

Count even and odd numbers

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

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 or uneven numbers.

 

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

here, we will discuss how to count odd and even numbers from an array in the C++ programming language

 

There are three programs given below

The common procedure of these programs

  • Initially, Declare an integer type array with maximum capacity(100) and declare variables i, size, odd and even
  • The user will enter the size of the array and store in the variable size to define the array size
  • Then, the user enters all the elements of the array according to the array size using loops
  • After that, use a loop for iterating elements of the array
  • Finally, the following  loops count even and odd number using if statements from the array

Count even and odd numbers in an array using for loop

Program 1

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

Then, it will count 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];
    int i,size,odd=0,even=0;

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

    //input the array elements
    cout<<"\nEnter elements of the array\n\n";


    for(i=0; i<size; i++)
    {
        cout<<"Enter the element arr["<<i<<"] :";
        cin>>arr[i];//read input from the user for array elements

    }


    for(i=0; i<size; i++)
    {
        if(arr[i]%2==0)
        {
            even++;
        }
        else{
            odd++;
        }

    }

    cout<<"\nTotal even numbers of an array :"<<even<<"\n";
    cout<<"Total odd numbers of an array : "<<odd;
     getch();
    return 0;
}

 

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

Enter the size of the array
10
Enter elements of the array

Enter the element arr[0] : 45
Enter the element arr[1] : 24
Enter the element arr[2] : 68
Enter the element arr[3] : 99
Enter the element arr[4] : 95
Enter the element arr[5] : 27
Enter the element arr[6] : 88
Enter the element arr[7] : 83
Enter the element arr[8] : 58
Enter the element arr[9] : 75

Total even numbers of an array :4
Total odd numbers of an array : 6

 

Program 2

Count even and odd numbers in an array 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 count 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];
    int i,size,odd=0,even=0;

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

    //input the array elements
    cout<<"\nEnter elements of the array\n\n";

    i=0;
    while(i<size)
    {
        cout<<"Enter the element arr["<<i<<"] :";
        cin>>arr[i];//read the input from user for array elements
         i++;
    }

     i=0;
    while(i<size)
    {
        if(arr[i]%2==0)
        {
            even++;
        }
        else{
            odd++;
        }
         i++;
    }

    cout<<"\nTotal even numbers of an array :"<<even<<"\n";
    cout<<"Total odd numbers of an array : "<<odd;
     getch();
    return 0;
}

 

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

Enter the size of the array
10
Enter elements of the array

Enter the element arr[0] : 90
Enter the element arr[1] : 88
Enter the element arr[2] : 76
Enter the element arr[3] : 53
Enter the element arr[4] : 24
Enter the element arr[5] : 12
Enter the element arr[6] : 59
Enter the element arr[7] : 82
Enter the element arr[8] : 45
Enter the element arr[9] : 56

Total even numbers of an array :7
Total odd numbers of an array : 3

 

Program 3

Count even and odd numbers in an array 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 count the even and odd numbers from this array using the do-while loop in C++

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

int main()
{
    int arr[100];
    int i,size,odd=0,even=0;

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

    //input the array elements
    cout<<"\nEnter elements of the array\n\n";

    i=0;

     do{
        cout<<"Enter the element arr["<<i<<"] :";
        cin>>arr[i];//read the input from user for array elements
         i++;
    }while(i<size);

     i=0;

    do{
        if(arr[i]%2==0)
        {
            even++;
        }
        else{
            odd++;
        }
         i++;
    }while(i<size);

    cout<<"\nTotal even numbers of an array :"<<even<<"\n";
    cout<<"Total odd numbers of an array : "<<odd;
     getch();
    return 0;
}


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

Enter the size of the array
10
Enter elements of the array

Enter the element arr[0] : 123
Enter the element arr[1] : 234
Enter the element arr[2] : 345
Enter the element arr[3] : 456
Enter the element arr[4] : 567
Enter the element arr[5] : 678
Enter the element arr[6] : 789
Enter the element arr[7] : 890
Enter the element arr[8] : 987
Enter the element arr[9] : 876

Total even numbers of an array :5
Total odd numbers of an array : 5

Similar post

Java code to compute even and odd numbers of an array

C code to compute even and odd numbers of an array

 

 

Suggested for you

Data type in C++

Variable in C++

Operator in C++

for loop in C++

while loop in C++

do-while loop in C++

If statements in C++

Single dimension array in C++

 

 

Java code to count even and odd numbers of an array
Display even and odd numbers without if statement in Java
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.

View Comments

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago