Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

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

NumberEven/OddStored In
5Oddodd[]
8Eveneven[]
10Eveneven[]
13Oddodd[]
2Eveneven[]

Output

Even numbers:
8 10 2

Odd numbers:
5 13

Time Complexity

OperationComplexity
Reading InputO(n)
Separating Even and Odd NumbersO(n)
Printing Even NumbersO(n)
Printing Odd NumbersO(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.

Comments

Popular Posts

🌙