Write a C++ fragment that prompts the user to enter integers one at a time, keeping track of the total number of odd numbers entered. If a negative number is entered, stop asking for numbers and print the result. Assume all needed headers are included.
#include <iostream>
using namespace std;
int
main() {
int odd_count = 0, input;
while (true) {
cout << "Enter an Integer:
";
cin >> input;
if (input < 0) {
break;
} else if (input % 2 != 0) //If the number is
odd
{
// Increase the counter
by one
odd_count++;
}
}
cout << "There were a total of " << odd_count <<
" odd numbers.";
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.