In this tutorial you will learn about the C++ program to find even and odd elements in array and its application with practical example.
C++ program to find even and odd elements in the array
In this tutorial, we will learn to create a C++ program that will find even and odd elements in the array using C++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ programming.
- For loop in C++ programming.
- Conditional Statements in C++ programming.
- Arithmetic operations in C++ Programming.
Even and Odd Numbers:-
The Even numbers are those numbers that are completely divisible by 2. which means the reminder should be zero. And the Odd numbers are those numbers that are not completely divisible by 2.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Storing in array. 4. Calculating the Even and the odd numbers from the array. 5. Printing the result numbers. 6. End Program. |
Program to find even and odd elements in the array
In C++ programming, it is possible to take integer input from the user and find even and odd elements in the array with the help of a very small amount of code. We will pass that array to a condition to find even and odd numbers from the array.
With the help of the below program, we can Find Even & Odd elements in the array.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
/* C++ Program to Find Even and Odd Numbers using array */ #include<iostream> using namespace std; int main() { //declaring the variables for the Program int arry[20],even[20],odd[20],i,j=0,k=0,size; // taking the size of the array cout<<"Enter the size of the array in number :: "; cin>>size; cout<<"\nEnter the elements of array :: \n"; for(i=0; i<size; i++) { //taking the elements of the array cout<<"\nEnter arry["<<i<<"] Element :: "; cin>>arry[i]; } cout<<"\nStored Data in array :: \n\n"; for(i=0;i<size;i++) { cout<<" "<<arry[i]<<" "; } for(i=0; i<size;i++) { if(arry[i]%2==0) { even[j]=arry[i]; j++; } else { odd[k]=arry[i]; k++; } } //printing the elements of the array even and odd cout<<"\n\nEven Elements in array are :: \n\n"; for(i=0; i<j ;i++) { cout<<" "<<even[i]<<" "; } cout<<"\n\nOdd Elements in array are :: \n\n"; for(i=0; i<k; i++) { cout<<" "<<odd[i]<<" "; } cout<<"\n"; return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- arry[20] = it will hold the integer value.
- even[20] = it will hold the integer value.
- odd[20] = it will hold the integer value.
- i = it will hold the integer value.
- j = it will hold the integer value.
- k = it will hold the integer value.
- size = it will hold the integer value.
Program Logic Code and print the even & odd numbers Separately.