The program is a while loop and it's suppose to count how many odd and even numbers I've entered before I enter 0. My instructor provided us with to sample outputs to work with for this assignment. The first output prints out correctly, but the second doesn't.
#include<iostream>
using namespace std;
int main()
{
int number, even = 0, odd = 0;
cout << "Enter a non-zero integer (0 to quit): ";
cin >> number;
while(number != 0)
{
cout << "Enter a non-zero integer (0 to quit): ";
cin >> number;
if(number%2==0)
even++;
else
odd++;
}
cout << "-----------------------------------------" << endl;
cout << "Even Count: " << even << endl;
cout << "Odd Count: " << odd << endl;
cout << "-----------------------------------------" << endl;
return 0;
}
The sample outputs below are the correct ones:
Sample Run 1:
Enter a non-zero integer (0 to quit): 4
Enter a non-zero integer (0 to quit): 4
Enter a non-zero integer (0 to quit): 6
Enter a non-zero integer (0 to quit): 7
Enter a non-zero integer (0 to quit): 0
-----------------------------------------
Even Count: 3
Odd Count: 1
-----------------------------------------
Sample Run 2:
Enter a non-zero integer (0 to quit): 89
Enter a non-zero integer (0 to quit): 55
Enter a non-zero integer (0 to quit): 56
Enter a non-zero integer (0 to quit): 1
Enter a non-zero integer (0 to quit): 2
Enter a non-zero integer (0 to quit): 0
-----------------------------------------
Even Count: 2
Odd Count: 3
-----------------------------------------
And here's how sample 2 prints out in my program. As you can see, it says there's 3 even numbers when there's actually 2, and it says there's 2 odd numbers when there's actually 3:
Question
jamesbrad288
The program is a while loop and it's suppose to count how many odd and even numbers I've entered before I enter 0. My instructor provided us with to sample outputs to work with for this assignment. The first output prints out correctly, but the second doesn't.
#include<iostream> using namespace std; int main() { int number, even = 0, odd = 0; cout << "Enter a non-zero integer (0 to quit): "; cin >> number; while(number != 0) { cout << "Enter a non-zero integer (0 to quit): "; cin >> number; if(number%2==0) even++; else odd++; } cout << "-----------------------------------------" << endl; cout << "Even Count: " << even << endl; cout << "Odd Count: " << odd << endl; cout << "-----------------------------------------" << endl; return 0; }The sample outputs below are the correct ones:
Sample Run 1:
Enter a non-zero integer (0 to quit): 4
Enter a non-zero integer (0 to quit): 4
Enter a non-zero integer (0 to quit): 6
Enter a non-zero integer (0 to quit): 7
Enter a non-zero integer (0 to quit): 0
-----------------------------------------
Even Count: 3
Odd Count: 1
-----------------------------------------
Sample Run 2:
Enter a non-zero integer (0 to quit): 89
Enter a non-zero integer (0 to quit): 55
Enter a non-zero integer (0 to quit): 56
Enter a non-zero integer (0 to quit): 1
Enter a non-zero integer (0 to quit): 2
Enter a non-zero integer (0 to quit): 0
-----------------------------------------
Even Count: 2
Odd Count: 3
-----------------------------------------
And here's how sample 2 prints out in my program. As you can see, it says there's 3 even numbers when there's actually 2, and it says there's 2 odd numbers when there's actually 3:
-----------------------------------------
Even Count: 3
Odd Count: 2
-----------------------------------------
Thanks!
Edited by jamesbrad288Link to comment
Share on other sites
3 answers to this question
Recommended Posts