In: Computer Science
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following:
You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
//Declaring constant
const int SENTINEL=-999;
//Declaring variables
int num,cntEven=0,cntOdd=0,evenSum=0,oddSum=0;
//Getting the input entered by the user
cout<<"Enter a number :";
cin>>num;
/* This while loop continues to execute
* until the user enters SENTINEL
*/
while(num!=SENTINEL)
{
if(num>0)
{
if(num%2==0)
{
cntEven++;
evenSum+=num;
}
else if(num%2!=0)
{
cntOdd++;
oddSum+=num;
}
}
//Getting the input entered by the user
cout<<"Enter a number :";
cin>>num;
}
//Displaying the output
cout<<"\nSum of all even numbers
:"<<evenSum<<endl;
cout<<"Sum of all odd numbers
:"<<oddSum<<endl;
cout<<"Count of all even numbers
:"<<cntEven<<endl;
cout<<"Count of all odd numbers
:"<<cntOdd<<endl;
return 0;
}
___________________________
Output:
_______________Could you plz rate me well.Thank
You