First N
First N
First N
array.
#include<iostream>
int main(){
int n;
cin>>n;
int array[n];
for(int i=0;i<n;i++)
cin>>array[i];
int max=array[0];
for(int i=0;i<=n;i++)
if(array[i]>max){
max=array[i];
}}
return 0;
#include<iostream>
int main(){
int pos,e;
int n;
cin>>n;
int array[n];
for(int i=0;i<n;i++)
cin>>array[i];
cin>>pos;
cin>>e;
pos--;
for(int i=n-1;i>=pos;i--)
array[i+1]=array[i];
array[pos]=e;
n++;
for(int i=0;i<n;i++)
cout<<array[i]<<" ";
return 0;
#include<iostream>
int main(){
cout<<"Zahra Bilal";
int sum=0;
int n;
cin>>n;
int array[n];
for(int i=0;i<n;i++)
cin>>array[i];
sum=sum+array[i];
cout<<"sum="<<sum<<endl;
return 0;
#include<iostream>
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int array[n];
min = array[i];
cout << "Minimum number is: " << min << endl;
return 0;}
#include<iostream>
using namespace std;
int main() {
int r,c;
cin >>r>>c;
int M1[r][c],M2[r][c],R[r][c];
R[i][j]=M1[i][j]+M2[i][j];
cout<<R[i][j]<<" ";
cout<<endl;
return 0;
}
Program no 6: Write a program to implement stack using array.
#include <iostream>
if (top>=n-1)
else{
top++;
stack[top] = val;
void pop(){
if (top<=-1)
else{
cout << "The popped element is " << stack[top] << endl;
top--;
void display(){
if(top>=0){
else
int main(){
int ch,val;
cout<<"Zahra Bilal"<<endl;
cout<<"1.Push in stack"<<endl;
cout<<"3.display stack"<<endl;
cout<<"4.exit"<<endl;
do{
cin>>ch;
switch(ch){
case 1 :{
cin>>val;
push(val);
break;
case 2 :{
pop();
break;
case 3 :{
display();
break;
case 4 :{
cout<<"Exit"<<endl;
break;
default:{
cout<<"invalid choice"<<endl;
}while(ch != 4);
}
Program no 7: Write a C++ program to perform following
operations on stack using array.
#include <iostream>
bool isFull() {
bool isEmpty() {
if (isFull())
cout << "The stack is full" << endl;
else {
top++;
stack[top] = val;
cout << "Pushed " << val << " into the stack." << endl;
void pop() {
if (isEmpty())
else {
cout << "The popped element is " << stack[top] << endl;
top--;
int stackSize() {
return top + 1;
void displayTopElement() {
if (!isEmpty()) {
cout << "The top element is: " << stack[top] << endl;
} else {
void displayFromTopToBottom() {
if (!isEmpty()) {
cout << "Stack elements from top to bottom: ";
} else {
void displayFromBottomToTop() {
if (!isEmpty()) {
} else {
int main() {
cout << "3. Display stack from top to bottom" << endl;
do {
switch (ch) {
case 1: {
push(val);
break;
case 2: {
pop();
break;
case 3: {
displayFromTopToBottom();
break;
case 4: {
break;
case 5: {
cout << "The size of the stack is: " << stackSize() << endl;
break;
}
case 6: {
displayTopElement();
break;
case 7: {
displayFromBottomToTop();
break;
default: {
return 0;
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int new_data) {
this->data = new_data;
this->next = NULL;
};
class LinkedList {
Node* head;
public:
LinkedList() {
head = NULL;
bool isEmpty() {
new_node->next = head;
head = new_node;
cout << new_data << " added to the list." << endl;
}
void deleteNode(int key) {
head = temp->next;
delete temp;
cout << key << " deleted from the list." << endl;
return;
prev = temp;
temp = temp->next;
if (temp == NULL) {
cout << key << " not found in the list." << endl;
return;
prev->next = temp->next;
delete temp;
cout << key << " deleted from the list." << endl;
void display() {
if (isEmpty()) {
return;
temp = temp->next;
};
int main() {
LinkedList list;
do {
cout << "2. Delete an integer from the list" << endl;
switch (choice) {
case 1:
list.push(value);
break;
case 2:
list.deleteNode(value);
break;
case 3:
list.display();
break;
case 4:
break;
default:
return 0;
class Node {
public:
int data;
Node* next;
Node(int new_data) {
this->data = new_data;
this->next = NULL;
};
class Stack {
Node* head;
public:
bool isEmpty() {
if (!new_node) {
new_node->next = head;
head = new_node;
void pop() {
if (this->isEmpty()) {
else {
head = head->next;
delete temp;
int peek() {
if (!isEmpty())
return head->data;
else {
return INT_MIN;
};
int main() {
Stack stack;
do {
switch (choice) {
case 1:
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
if (!stack.isEmpty())
else
break;
case 4:
if (stack.isEmpty())
else
break;
case 5:
break;
default:
return 0;
Program no 10: Input a string and reverse it using stack.String - " <<
str;
#include <iostream>
class Stack {
char stack[MAX_SIZE];
int top;
public:
Stack() {
top = -1;
}
bool isFull() {
bool isEmpty() {
if (isFull()) {
return;
stack[++top] = value;
char pop() {
if (isEmpty()) {
return '\0';
return stack[top--];
};
Stack stack;
char ch = input[i];
stack.push(ch);
}
string reversed = "";
while (!stack.isEmpty()) {
reversed += stack.pop();
return reversed;
int main() {
string str;
getline(cin, str);
return 0;