1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
// MAIN_CPP
#include <iostream>
using namespace std;
#include <iomanip>
using std::setprecision;
int main()
{
int counter_even = 0,
counter_odd = 0,
input = 0,
sum_even = 0,
sum_odd = 0;
double average_even = 0,
average_odd = 0;
cout << "Please input a number to check on. If you enter -1, the programm will stop." << endl;
cin >> input;
do
{
if (input % 2 == 0)
{
cout << "This number is even!" << endl;
counter_even++;
sum_even += input;
}
else if (input % 2 != 0)
{
cout << "This number is odd!" << endl;
counter_odd++;
sum_odd += input;
}
else
cout << "There was an error during the even/odd identification!" << endl;
cout << "Please input a number to check on. If you enter -1, the programm will stop." << endl;
cin >> input;
} while (input != -1);
if (counter_even != 0)
average_even = static_cast<double>(sum_even) / counter_even;
else average_even = 0;
if (counter_odd != 0)
average_odd = static_cast<double>(sum_odd) / counter_odd;
else
average_odd = 0;
cout << "There were " << counter_even << " even number(s), and their average was " << setprecision(2) << fixed << average_even << "!" << endl;
cout << "There were " << counter_odd << " odd number(s), and their average was " << setprecision(2) << fixed << average_odd << "!" << endl;
if (counter_even > counter_odd)
cout << "There were more even numbers than odd numbers." << endl;
else if (counter_even < counter_odd)
cout << "There were more odd numbers than even numbers." << endl;
else
cout << "There was the same amount of even and odd numbers." << endl;
if (average_even > average_odd)
cout << "even numbers average is higher." << endl;
else if (average_even < average_odd)
cout << "odd numbers average is higher" << endl;
else
cout << "The average of the even numbers is as high as of the odd ones." << endl;
return 0;
}
/*
Please input a number to check on. If you enter -1, the programm will stop.
3
This number is odd!
Please input a number to check on. If you enter -1, the programm will stop.
6
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
12
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
4
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
13
This number is odd!
Please input a number to check on. If you enter -1, the programm will stop.
12
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
8
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
2
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
133
This number is odd!
Please input a number to check on. If you enter -1, the programm will stop.
-1
There were 6 even number(s), and their average was 7.33!
There were 3 odd number(s), and their average was 49.67!
There were more even numbers than odd numbers.
odd numbers average is higher
Press any key to continue
*/
|