Answer to Question #308834 in C++ for Chubby

Question #308834

Enter the maximum number: 21


Even numbers are :2 4 6 8 10 12 14 16 18 20

Odd numbers are :1 3 5 7 9 11 13 15 17 19 21



1
Expert's answer
2022-03-11T14:04:23-0500

#include <iostream>

#include <vector>


void print(const std::vector<int>& vec, const std::string& message) {

    std::cout << message;


    for (int a : vec) {

        std::cout << a << " ";

    }


    std::cout << std::endl;

}


int main() {


    size_t size;


    std::vector<int> even;

    std::vector<int> odd;


    std::cout << "Enter the maximum number: ";

    std::cin >> size;


    for (size_t i = 1; i <= size; ++i) {

        if (i % 2 == 0) {

            even.push_back(i);

        } else {

            odd.push_back(i);

        }

    }


    print(even, "Even numbers are: ");

    print(odd, "Odd numbers are: ");


    return 0;

}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog