Write a C++ program that asks the user for positive integers. Inform the user if
ID: 3563940 • Letter: W
Question
Write a C++ program that asks the user for positive integers. Inform the user if the number they entered was an odd or even number. Your program should tell the user each time after they enter whether the number was odd or even. At the end of your program, display, for the user, how many of their numbers were even numbers and how many were odd numbers. Also display the total for the odd numbers and the total for the even numbers. For example, if the user entered the numbers 3, 25, and 4 you would tell the user that 3 was odd, 25 was odd, and 4 was even. You would then tell the user that the count for the odd numbers entered was two (i.e. for 3 and 25) and the count for the even numbers entered was one (i.e. they only entered one number which was even). You would then tell them that the total for the odd numbers was 28 (adding together 3 and 25) and the total for the even numbers was 4 (only adding four to the even total).
Your program must utilize an appropriate loop structure and validate and process each number as it is entered. The program should stop when a 0 (zero) or negative number is entered.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int n,i,sumeven=0,sumodd=0;
int evenCount=0,oddCount=0;
while(true)
{
cout<<"Enter value a number:";
cin>>n;
if(n<=0)
break;
if(n%2==0) {
sumeven+=n;
cout<<n<<" was even ";
evenCount++;
}
else {
sumodd+=n;
cout<<n<<" was odd ";
oddCount++;
}
}
cout<<" Total Number of Even Numbers :"<<evenCount;
cout<<" Total Number of Odd Numbers : "<<oddCount;
cout<<" Sum of even Numbers is :"<<sumeven;
cout<<" SUm of odd Numbers is :"<<sumodd;
}
======================
Output
=======================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.