Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! 🚀 🔹Learn advanced coding techniques in C& C++ 🔹Master Linux internals & shell scripting 🔹Deep dive into Data Structures & Algorithms 🔹Explore Embedded Systems & Microcontrollers (8051,UART, RTOS) 🔹Get hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
C++ Program to Separate Even and Odd Numbers from a Vector
C++ Program to Separate Even and Odd Numbers from a Vector
Introduction
In this C++ program, we will learn how to separate even and odd numbers from a vector. The program takes the size of the vector as input, stores the elements, and then divides them into two separate arrays—one for even numbers and another for odd numbers. Finally, it displays both groups separately.
C++ Program
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num;
cout << "Enter the size of the vector:" << endl;
cin >> num;
vector<int> v(num);
int even[num], odd[num];
int evencount = 0, oddcount = 0;
cout << "Enter " << num << " elements in vector:" << endl;
for(int i = 0; i < v.size(); i++)
{
cin >> v[i];
if(v[i] % 2 == 0)
{
even[evencount++] = v[i];
}
else
{
odd[oddcount++] = v[i];
}
}
cout << "Even numbers are:" << endl;
for(int i = 0; i < evencount; i++)
{
cout << even[i] << " ";
}
cout << "\nOdd numbers are:" << endl;
for(int i = 0; i < oddcount; i++)
{
cout << odd[i] << " ";
}
return 0;
}
Sample Output
Enter the size of the vector:
6
Enter 6 elements in vector:
10 15 8 21 6 11
Even numbers are:
10 8 6
Odd numbers are:
15 21 11
Explanation
Step 1
Read the size of the vector from the user.
int num;
cin >> num;
Step 2
Create a vector of the given size.
vector<int> v(num);
Step 3
Create two arrays to store even and odd numbers separately.
int even[num], odd[num];
Step 4
Initialize counters to keep track of how many even and odd numbers are stored.
int evencount = 0;
int oddcount = 0;
Step 5
Read each element of the vector and check whether it is even or odd.
if(v[i] % 2 == 0)
{
even[evencount++] = v[i];
}
else
{
odd[oddcount++] = v[i];
}
If the number is divisible by 2, it is stored in the even array.
Otherwise, it is stored in the odd array.
Step 6
Display all even numbers.
for(int i = 0; i < evencount; i++)
{
cout << even[i] << " ";
}
Step 7
Display all odd numbers.
for(int i = 0; i < oddcount; i++)
{
cout << odd[i] << " ";
}
Dry Run
Input
Vector = {5, 8, 10, 13, 2}
Processing
| Number | Even/Odd | Stored In |
|---|---|---|
| 5 | Odd | odd[] |
| 8 | Even | even[] |
| 10 | Even | even[] |
| 13 | Odd | odd[] |
| 2 | Even | even[] |
Output
Even numbers:
8 10 2
Odd numbers:
5 13
Time Complexity
| Operation | Complexity |
|---|---|
| Reading Input | O(n) |
| Separating Even and Odd Numbers | O(n) |
| Printing Even Numbers | O(n) |
| Printing Odd Numbers | O(n) |
Overall Time Complexity: O(n)
Space Complexity
Vector: O(n)
Even Array: O(n)
Odd Array: O(n)
Overall Auxiliary Space Complexity: O(n)
Key Points
Uses a vector to store input elements.
Separates even and odd numbers in a single traversal.
Maintains the original order of elements.
Uses two counters to track the number of even and odd elements.
Efficient solution with O(n) time complexity.
Keywords
C++ Vector, Even and Odd Numbers in C++, Separate Even and Odd Numbers, Vector Program in C++, C++ Arrays, STL Vector, C++ Programming Examples, C++ Interview Programs, Data Structures in C++, C++ Coding Practice.
Conclusion
This program demonstrates how to separate even and odd numbers from a vector efficiently using a single loop. It is a simple yet useful example for understanding vectors, arrays, conditional statements, and basic data manipulation in C++. The algorithm runs in O(n) time, making it efficient for processing large collections of numbers.
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment