Create a program that get 10 even numbers from user
Hi. I need to enter numbers not more than 10 but it failed. I used while but it kept asking me to enter numbers.
The output should be like this:
Please key in 10 even numbers:
20
3
This is odd number! 3
12
30
44
24
96
10
9
This is odd number! 9
123
This is odd number! 123
36
78
94
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
|
#include<iostream>
using namespace std;
class ADTstack
{
private:
int stack[10];
int topstack;
public:
ADTstack()
{
topstack = -1;
}
int empty()
{
if(topstack==-1)
return 1;
else
return 0;
}
int full()
{
if(topstack==9)
return 1;
else
return 0;
}
void push(int num)
{
if (!full())
{
topstack++;
stack[topstack] = num;
}
else
{
cout<<"Stack is Full"<<endl;
}
}
int pop()
{
int num;
if(!empty())
{
num = stack[topstack];
topstack--;
return num;
}
else
{
cout<<"Stack is Empty"<<endl;
return 0;
}
}
};
int main()
{
ADTstack st;
int num;
cout << "Please key in 10 even numbers:" << endl;
cin >> num;
if (num % 2 == 0)
{
cout << st.pop() << endl;
}
else
{
cout << "This is odd number!" <<endl;
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
while (!st.full())
{
cin >> num;
if (num % 2 == 0)
{
st.push(num);
}
else
{
cout << "This is odd number!" <<endl;
}
}
|
Topic archived. No new replies allowed.