Answer :
Answer:
- #include <iostream>
- using namespace std;
- int main()
- {
- int value;
- int oddCount = 0;
- int evenCount = 0;
- cout<<"Input a positive number: ";
- cin>> value;
- if(value == -1){
- cout<<"NO input is entered!";
- return 0;
- }
- if(value < -1) {
- cout<<"The input number must be positive";
- return 0;
- }
- while(value != -1){
- if(value % 2 == 0){
- evenCount++;
- }else{
- oddCount++;
- }
- cout<<"Input a positive number: ";
- cin>>value;
- if(value < -1){
- cout<<"Wrong input number\n";
- return 0;
- }
- }
- cout<<"Number of odd number: "<< oddCount<<"\n";
- cout<<"Number of even number: "<< evenCount;
- return 0;
- }
Explanation:
Firstly, create three variables, value, oddCount and evenCount (Line 7-9). The value variable is to hold the input number from user (Line 11 -12). oddCount and evenCount are to track the number of occurrence of even and odd number of the user input (Line 21-25).
The sentinel value here is -1 and therefore if user enter -1 as first input, the message "NO input is entered!" will be displayed. (Line 14).
If user input a negative value other than -1 , display the alert message to user and terminate the program (Line 19-22).
We check if the value modulus by two is zero, this means it is an even number and then increment evenCount by one (Line 26-27). Otherwise increment oddCount by one (Line 28-29).
After that prompt the user to input another number again and repeat the loop (Line 31-36). So long as the user don't enter -1, the while loop will just keep running.
At last, print the number of odd and even number (Line 40-41).