Answer to Question #182183 in C++ for khairul

Question #182183

 Write a program that reads a given set of integers and then prints the number of odd

and even integers. It also outputs the number lof zeros.

Sample output: (The user input is bolded)

Please enter 20 integers, positive, negative, or zeros.

0 0 -2 -3 -5 6 7 8 0 3 0 -23 -8 0 2 9 0 12 67 54

hembers you entered arer

7 8030-2990 29 0 12 67 54

There are 13 evens, which includes 6 zeros.

The number of odd numbers is 7.


1
Expert's answer
2021-04-16T16:11:13-0400
#include <string>
#include <iostream>
#include <sstream>
#include <utility>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
    string line;
    cout << "Please enter 20 integers, positive, negative, or zeros.\n";
    getline(cin, line);
    istringstream this_line(line);
    istream_iterator<int> begin(this_line), end;
    vector<int> values(begin, end);
    int even = 0, odd = 0, zeros = 0;
    cout << "The numbers you entered are\n";
    for (auto i = values.begin(); i != values.end(); ++i){
        int n = *i;
        cout << n << " ";
        if(n == 0) {
            zeros++;
        }
        else if (n % 2 == 0) {
            even++;
        }
        else {
            odd++;
        }
    }
    cout << "\nThere are "<<even + zeros<< " evens, which includes "<<zeros<<" zeros"<<endl;
    cout << "The number of odd numbers is "<<odd<<endl;
}

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
APPROVED BY CLIENTS