Write a C++ program called PositiveNumbers.cpp.
The program must do the following:
ï‚· Prompt user to enter a positive number that is greater than 0 or a -1 to quit (See Figure 4.1).
NB: Make use of a post-test loop
ï‚· If the number is within the range, determine the following :
o how many numbers are entered,
o the total (sum) of all numbers entered,
o determine how many are even numbers and,
o determine the highest number entered.
ï‚· If the number is invalid display and appropriate message as indicated in Figure 4.2.
ï‚· This process must be repeated until an invalid number is entered (-1).When the process terminates, display the following (See Figure 4.1 and Figure 4.3)
o the total of the numbers entered,
o the total number of even numbers,
o the highest number and
o the sum of all numbers entered.
PositiveNumbers.cpp
#include <iostream>
using namespace std;
int main() {
int n;
bool flag = true;
int count = 0;
int sum = 0;
int even = 0;
int max = 0;
while ( flag ) {
cout << "Enter an integer number: ";
cin >> n;
if (n > 0) {
count = count + 1;
sum = sum + n;
if (n % 2 == 0) even = even + 1;
if (max < n) max = n;
} else if (n != -1) {
cout << "Invalid integer number!" << endl;
} else {
flag = false;
}
}
cout << "the total of the numbers entered: " << count << endl;
cout << "the total number of even numbers: " << even << endl;
cout << "the highest number: " << max << endl;
cout << "the sum of all numbers entered: " << sum << endl;
return 0;
}
Comments
Leave a comment