#include <iostream>
using namespace std;
#define MAX 5
int stack[MAX], top = -1;
void push(int val) {
if (top == MAX - 1)
cout << "Stack Overflow!" << endl;
else {
stack[++top] = val;
cout << "Pushed " << val << " to stack." << endl;
void pop() {
if (top == -1)
cout << "Stack Underflow!" << endl;
else
cout << "Popped " << stack[top--] << " from stack." << endl;
void display() {
if (top == -1)
cout << "Stack is empty." << endl;
else {
cout << "Stack elements:" << endl;
for (int i = top; i >= 0; i--)
cout << stack[i] << " ";
cout << endl;
int main() {
int choice, val;
do {
cout << "\n1) Push\n2) Pop\n3) Display\n4) Exit\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> val;
push(val);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 4);
return 0;