Write a C++ program that keeps taking positive integers from the user until user enters -1. The program counts and displays how may even and odd numbers are entered by the user. See the following example. Enter a number (enter -1 to exit): 5 Enter a number (enter -1 to exit): 34 Enter a number (enter -1 to exit): 4 Enter a number (enter -1 to exit): -1 You have entered 2 even and 1 odd numbers.
#include<iostream>
using namespace std;
int main(){
int x;
int even = 0;
int odd = 0;
do{
cout<<"Enter a number (enter -1 to exit): ";
cin>>x;
if(x%2==0){
even++;
}
else{
odd++;
}
}while(x!=-1);
cout<<" You have entered "<<even<<" even and "<<odd - 1<<" odd numbers.";
}
Comments
Leave a comment