25.4k views
15 votes
Write a loop that reads positive integers from console input, printing out those values that are even, separating them with spaces, and that terminates when it reads an integer that is not positive. Declare any variables that are needed.

User AMJay
by
3.3k points

1 Answer

6 votes

Hello, since you did not specify a programming language, I wrote this algorithm in C++. Good luck!

Code:

#include <iostream>

#include <vector>

std::vector<int> v;

int main(int argc, char* argv[]) {

while(1) {

int temp;

std::cout << "\\Enter a number: ";std::cin>>temp;

if(temp<0) {

std::cout << "\\Even number(s) is/are:\\---------------------\\";

for(int i=0;i<v.size();i++) {

if(v.at(i)%2==0) std::cout << v[i] << " ";

else continue;

}

std::cout << std::endl;

break;

}else {

v.push_back(temp);

}

}

return 0;

}

Write a loop that reads positive integers from console input, printing out those values-example-1
User Krunal Nagvadia
by
3.3k points