Hi this is my first time creating a C++ code. The task is to ask the user for ten numbers, determine whether each is even or odd and determine the max, min, and sum of the 10 numbers.
#include <iostream>
usingnamespace std;
int main ()
{
int min, max, sum, n;
int x = 1; //set a starting number for my loop
sum = 0; //starting sum of 0
min = n;
max = n;
while (x <= 10) //loop will run 10 times
{
cout << "Please enter a number:" << endl;
cin >> n;
//enters if-else to determine whether n is even or odd
if (n % 2 == 0)
{
cout << n << " is even." <<endl;
}
else
{
cout << n << " is odd." << endl;
}
//if statements to determine and set min and max
if (n < min)
{
min = n;
}
if (n > max)
{
max = n;
}
sum = sum + n;
x = x + 1;
}
cout << "The minimum is: " << min << endl;
cout << "The maximum is: " << max << endl;
cout << "The sum is: " << sum << endl;
return 0;
}
Above is my code but for some reason I keep getting 32767 as my maximum no matter what.
n is uninitialized so this is undefined behavior. You should set the min to a large number (or the first number being input) and then set max to a small number (or the first number being input).