Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

First N

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Program no 1: write a program to print the largest number in an

array.

#include<iostream>

using namespace std;

int main(){

int n;

cout<<"Enter the number of elements:";

cin>>n;

cout<<"Enter the elements:";

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];

}}

cout<<"maximum number is"<<max<<endl;

return 0;

Program no 2: Write a program to insert an element in an array.

#include<iostream>

using namespace std;

int main(){

int pos,e;
int n;

cout<<"Enter the number of elements:";

cin>>n;

cout<<"Enter the elements:"<<endl;

int array[n];

for(int i=0;i<n;i++)

cin>>array[i];

cout<<"Enter the position at which you want to insert an element"<<endl;

cin>>pos;

cout<<"Enter the elements:"<<endl;

cin>>e;

pos--;

for(int i=n-1;i>=pos;i--)

array[i+1]=array[i];

array[pos]=e;

n++;

cout<<"Ater inserting an element"<<endl;

for(int i=0;i<n;i++)

cout<<array[i]<<" ";

return 0;

Program no 3: Write a program to find sum of elements in an


array.

#include<iostream>

using namespace std;

int main(){
cout<<"Zahra Bilal";

int sum=0;

int n;

cout<<"Enter the number of elements:";

cin>>n;

cout<<"Enter the elements:"<<endl;

int array[n];

for(int i=0;i<n;i++)

cin>>array[i];

sum=sum+array[i];

cout<<"sum="<<sum<<endl;

return 0;

Program no 4: Write a program to find smallest number in an


array.

#include<iostream>

using namespace std;

int main() {

cout << "Zahra Bilal" << endl;

int n;
cout << "Enter the number of elements: ";

cin >> n;

cout << "Enter the elements: ";

int array[n];

for (int i = 0; i < n; i++) {

cin >> array[i];

int min = array[0];

for (int i = 1; i < n; i++) {

if (array[i] < min) {

min = array[i];

cout << "Minimum number is: " << min << endl;

return 0;}

Program no 5: Write a program that provides the sum of two


matrices.

#include<iostream>
using namespace std;

int main() {

cout << "Zahra Bilal" << endl;

int r,c;

cout << "Enter the size of matrix: ";

cin >>r>>c;

int M1[r][c],M2[r][c],R[r][c];

cout <<endl<< "Enter the elements of 1 matrix: ";

for (int i = 0; i < r; i++) {

for(int j=0 ; j<c ; j++)

cin >> M1[i][j];

cout <<endl<< "Enter the elements of 2 matrix: ";

for (int i = 0; i < r; i++) {

for(int j=0 ; j<c ; j++)

cin >> M2[i][j];

cout<<endl<<"Sum of two matrices "<<endl;

for (int i = 0; i < r; i++) {

for(int j=0 ; j<c ; j++) {

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>

using namespace std;

const int n=100;

int stack[n], top=-1;

void push(int val){

if (top>=n-1)

cout << "the stack is full" << endl;

else{

top++;

stack[top] = val;

void pop(){

if (top<=-1)

cout << "Error: the stack is empty"<< endl;

else{
cout << "The popped element is " << stack[top] << endl;

top--;

void display(){

if(top>=0){

cout << "Stack elements are: ";

for(int i=top; i>=0; i--)

cout << stack[i] << " ";

cout << endl;

else

cout << "Stack is empty";

int main(){

int ch,val;

cout<<"Zahra Bilal"<<endl;

cout<<"1.Push in stack"<<endl;

cout<<"2.Pop from stack"<<endl;

cout<<"3.display stack"<<endl;

cout<<"4.exit"<<endl;

do{

cout<<endl<<"Enter your choice";

cin>>ch;

switch(ch){
case 1 :{

cout<<"Enter the value to be pushed:";

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.

a)Find size of stack

b)Display the top element of stack

c)Display stack from top to bottom

d)Display stack from bottom to top

#include <iostream>

using namespace std;

const int n = 100;

int stack[n], top = -1;

bool isFull() {

return top >= n - 1;

bool isEmpty() {

return top == -1;

void push(int val) {

if (isFull())
cout << "The stack is full" << endl;

else {

top++;

stack[top] = val;

cout << "Pushed " << val << " into the stack." << endl;

void pop() {

if (isEmpty())

cout << "Error: the stack is empty" << endl;

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 {

cout << "The stack is empty" << endl;

void displayFromTopToBottom() {

if (!isEmpty()) {
cout << "Stack elements from top to bottom: ";

for (int i = top; i >= 0; i--)

cout << stack[i] << " ";

cout << endl;

} else {

cout << "Stack is empty" << endl;

void displayFromBottomToTop() {

if (!isEmpty()) {

cout << "Stack elements from bottom to top: ";

for (int i = 0; i <= top; i++)

cout << stack[i] << " ";

cout << endl;

} else {

cout << "Stack is empty" << endl;

int main() {

int ch, val;

cout << "Zahra Bilal" << endl;

cout << "1. Push in stack" << endl;

cout << "2. Pop from stack" << endl;

cout << "3. Display stack from top to bottom" << endl;

cout << "4. Exit" << endl;

cout << "5. Display stack size" << endl;

cout << "6. Display top element of stack" << endl;


cout << "7. Display stack from bottom to top" << endl;

do {

cout << endl << "Enter your choice: ";

cin >> ch;

switch (ch) {

case 1: {

cout << "Enter the value to be pushed: ";

cin >> val;

push(val);

break;

case 2: {

pop();

break;

case 3: {

displayFromTopToBottom();

break;

case 4: {

cout << "Exit" << endl;

break;

case 5: {

cout << "The size of the stack is: " << stackSize() << endl;

break;

}
case 6: {

displayTopElement();

break;

case 7: {

displayFromBottomToTop();

break;

default: {

cout << "Invalid choice" << endl;

} while (ch != 4);

return 0;

Program no 8: Write a C++ program that uses function to


perform the following:

a)Create a singly linked list of integers

b)Delete a given integer fom the above linked list


c) Display the contents of the above list after deletion

#include <iostream>

using namespace std;

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() {

return head == NULL;

void push(int new_data) {

Node* new_node = new Node(new_data);

new_node->next = head;

head = new_node;

cout << new_data << " added to the list." << endl;

}
void deleteNode(int key) {

Node* temp = head;

Node* prev = NULL;

if (temp != NULL && temp->data == key) {

head = temp->next;

delete temp;

cout << key << " deleted from the list." << endl;

return;

while (temp != NULL && temp->data != key) {

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()) {

cout << "The list is empty." << endl;

return;

Node* temp = head;


cout << "List contents: ";

while (temp != NULL) {

cout << temp->data << " ";

temp = temp->next;

cout << endl;

};

int main() {

int choice, value;

LinkedList list;

do {

cout << "\nMenu:" << endl;

cout << "1. Add an integer to the list" << endl;

cout << "2. Delete an integer from the list" << endl;

cout << "3. Display the list" << endl;

cout << "4. Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter the value to add: ";

cin >> value;

list.push(value);

break;

case 2:

cout << "Enter the value to delete: ";


cin >> value;

list.deleteNode(value);

break;

case 3:

list.display();

break;

case 4:

cout << "Exiting..." << endl;

break;

default:

cout << "Invalid choice. Please try again." << endl;

} while (choice != 4);

return 0;

Program no 9: Write a program to implement stack using linked


list.
#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* next;

Node(int new_data) {

this->data = new_data;

this->next = NULL;

};

class Stack {

Node* head;

public:

Stack() { this->head == NULL; }

bool isEmpty() {

return head == NULL;

void push(int new_data) {

Node* new_node = new Node(new_data);

if (!new_node) {

cout << "\nStack Overflow";

new_node->next = head;

head = new_node;

void pop() {
if (this->isEmpty()) {

cout << "\nStack Underflow" << endl;

else {

Node* temp = head;

head = head->next;

delete temp;

int peek() {

if (!isEmpty())

return head->data;

else {

cout << "\nStack is empty";

return INT_MIN;

};

int main() {

Stack stack;

int choice, value;

do {

cout << "\nStack Operations Menu:" << endl;

cout << "1. Push" << endl;

cout << "2. Pop" << endl;

cout << "3. Peek (top element)" << endl;

cout << "4. Check if stack is empty" << endl;


cout << "5. Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter the value to push: ";

cin >> value;

stack.push(value);

break;

case 2:

stack.pop();

break;

case 3:

if (!stack.isEmpty())

cout << "Top element is " << stack.peek() << endl;

else

cout << "Stack is empty." << endl;

break;

case 4:

if (stack.isEmpty())

cout << "Stack is empty." << endl;

else

cout << "Stack is not empty." << endl;

break;

case 5:

cout << "Exiting..." << endl;

break;
default:

cout << "Invalid choice. Please try again." << endl;

} while (choice != 5);

return 0;

Program no 10: Input a string and reverse it using stack.String - " <<
str;

#include <iostream>

using namespace std;

const int MAX_SIZE = 100;

class Stack {

char stack[MAX_SIZE];

int top;

public:

Stack() {

top = -1;

}
bool isFull() {

return top >= MAX_SIZE - 1;

bool isEmpty() {

return top < 0;

void push(char value) {

if (isFull()) {

cout << "Stack Overflow" << endl;

return;

stack[++top] = value;

char pop() {

if (isEmpty()) {

cout << "Stack Underflow" << endl;

return '\0';

return stack[top--];

};

string reverseString(const string &input) {

Stack stack;

for (int i = 0; i < input.length(); ++i) {

char ch = input[i];

stack.push(ch);

}
string reversed = "";

while (!stack.isEmpty()) {

reversed += stack.pop();

return reversed;

int main() {

string str;

cout << "Enter a string: ";

getline(cin, str);

string reversedStr = reverseString(str);

cout << "Reversed string: " << reversedStr << endl;

return 0;

You might also like