A- 311
A- 311
Roll No : 311
Write a program to check give number is positive/negtive even or odd.
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter a number:";
cin>>num;
if(num == 0)
{
cout<<"it is zero";
}
else if(num % 2==0)
{
if(num>0)
{
cout<<"positive even number";
}
else
{
cout<<"negative even number";
}
}
else{
if(num>0)
{
cout<<"postive odd number";
}
else{
cout<<"negative odd number";
}
}
}
Description :
Write a program to print numbers from 1 to 30 and skip numbers from 11 to 20.
#include<iostream>
using namespace std;
int main()
{
for(int i = 1;i<30;i++)
{
if(i>=11&& i<=20)
{
continue;
}
cout<<i<<endl;
}
}
output :
#include <iostream>
}
void display(int array[], int size) {
cout << "arr[" << i << "]" << " = " << array[i] << "\n";
if (array[i] == element)
if (index == -1) {
} else {
int main() {
int size;
input(arr, size);
display(arr, size);
int element;
display(arr, size);
return 0;
output :
Description :
#include <iostream>
}
}
cout << "arr[" << i << "] = " << array[i] << "\n";
int main() {
int size;
display(arr, size);
int element;
cout << "Enter the element you want to search for: ";
int result = linearSearch(arr, size, element); // Fix assignment and missing semicolon
if (result != -1) {
cout << "Element found at index " << result << ".\n"; // Fix incorrect string concatenation
} else {
return 0;
Description :
#include <iostream>
cout << "arr[" << i << "] = " << array[i] << "\n";
int insert(int array[], int size, int element, int capacity, int index) {
}
int main() {
int size;
input(arr, size);
display(arr, size);
cout << "Enter index at which you want to insert the element: ";
if (result == 1) {
display(arr, size);
} else {
return 0;
Output :
Description :
#include <iostream>
cout << "arr[" << i << "] = " << array[i] << "\n";
}
cout << "\n";
return;
array[index] = newElement;
}
int main() {
int size;
input(arr, size);
display(arr, size);
display(arr, size);
return 0;
Output :
Description :
#include <iostream>
void push();
void pop();
void show();
int main() {
cin >> n;
cout << "********* Stack operations using array *********" << endl;
while (choice != 4) {
cout << "\nChoose one from the below options..." << endl;
switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
break;
default:
void push() {
int val;
if (top = = n - 1) {
cout << "\nOverflow" << endl; // Stack overflow if top reaches n-1
} else {
top = top + 1;
stack[top] = val;
void pop() {
if (top = = -1) {
} else {
void show() {
if (top = = -1) {
} else {
cout << stack[i] << endl; // Display stack elements from top to bottom
Output :
Description :
Array
#include <iostream>
using namespace std;
int main() {
int Arr[5] = {18, 30, 15, 70, 12}; // Declare and initialize the array
int i;
cout << "Elements of the array are:" << endl; // Use cout for output
for(i = 0; i < 5; i++) {
cout << "Arr[" << i << "] = " << Arr[i] << ", "; // Use cout for array element printing
}
output :
description :
#include <iostream>
using namespace std;
int main() {
int arr[20] = {18, 30, 15, 70, 12}; // Initialize array
int i, x, pos, n = 5;
Output :
Description :
output :
output :
Description :
Circular queue :
#include <iostream>
#define max 6
using namespace std;
int queue[max
int front = -1;
int rear = -1;
if (front == -1 && rear == -1) {
front = 0;
rear = 0;
queue[rear] = element;
}
else if ((rear + 1) % max == front) {
cout << "Queue is overflow.." << endl;
}
else {
rear = (rear + 1) % max;
queue[rear] = element;
}
void dequeue() {
if (front == -1 && rear == -1) {
cout << "\nQueue is underflow.." << endl;
}
else if (front == rear) {
cout << "\nThe dequeued element is " << queue[front] << endl;
front = -1;
rear = -1;
}
else {
cout << "\nThe dequeued element is " << queue[front] << endl;
front = (front + 1) % max;
}
}
void display() {
if (front == -1 && rear == -1) {
cout << "\nQueue is empty.." << endl;
}
else {
cout << "\nElements in the Queue are: ";
int i = front;
while (i != rear) {
cout << queue[i] << ", ";
i = (i + 1) % max;
}
cout << queue[rear] << endl;
}
}
int main() {
int choice = 1, x;
while (choice < 4 && choice != 0) {
cout << "\nPress 1: Insert an element";
cout << "\nPress 2: Delete an element";
cout << "\nPress 3: Display the elements";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the element to be inserted: ";
cin >> x;
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
cout << "Exiting program." << endl;
choice = 0;
break;
}
}
return 0;
}
Stack Operations
Push ():
#include <iostream>
using namespace std;
int top = -1
int stack[100];
void push(int val, int n) {
if (top = = n - 1) {
cout << "\nOverflow" << endl;
} else {
top = top + 1;
stack[top] = val;
}
}
int main() {
int n = 5;
push(10, n);
push(20, n);
push(30, n);
push(40, n);
push(50, n);
return 0; }
POP():
#include <iostream>
using namespace std;
int top = -1;
int stack[100];
int pop() {
if (top = = -1) {
cout << "Underflow" << endl;
return 0;
} else {
return stack[top--];
}
}
int main() {
stack[++top] = 10;
stack[++top] = 20;
cout << "Popped element: " << pop() << endl;
cout << "Popped element: " << pop() << endl;
cout << "Popped element: " << pop() << endl;
}
output :
#include <iostream>
#include <string>
using namespace std;
if (head->data == data) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* temp = head;
while (temp->next != nullptr && temp->next->data != data) {
temp = temp->next;
}
if (temp->next == nullptr) {
cout << "Node with data \"" << data << "\" not found." << endl;
return;
}
int main() {
Node* head = nullptr;
insertAtEnd(head, "Apple");
insertAtEnd(head, "Banana");
insertAtEnd(head, "Cherry");
insertAtBeginning(head, "Apricot");
}
Output :
#include <iostream>
#include <string>
if (head->data == data) {
Node* temp = head;
head = head->next;
if (head != nullptr) {
head->prev = nullptr;
} else {
tail = nullptr;
}
delete temp;
return;
}
int main() {
Node* head = nullptr;
Node* tail = nullptr;
return 0;
}
Assignmnet 6
In order :
Code :
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = NULL;
right = NULL;
}
};
int main() {
// Create a binary tree
Node* root = NULL;
root = insertNode(root, 50);
root = insertNode(root, 30);
root = insertNode(root, 70);
root = insertNode(root, 20);
root = insertNode(root, 40);
root = insertNode(root, 60);
root = insertNode(root, 80);
return 0;
}
Output:
Post order :
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = NULL;
right = NULL;
}
};
return root;
}
int main() {
// Create a binary tree
Node* root = NULL;
root = insertNode(root, 50);
root = insertNode(root, 30);
root = insertNode(root, 70);
root = insertNode(root, 20);
root = insertNode(root, 40);
root = insertNode(root, 60);
root = insertNode(root, 80);
return 0;
}
Output :
Pre order :
Code :
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = NULL;
right = NULL;
}
};
return root;
}
int main() {
// Create a binary tree
Node* root = NULL;
root = insertNode(root, 50);
root = insertNode(root, 30);
root = insertNode(root, 70);
root = insertNode(root, 20);
root = insertNode(root, 40);
root = insertNode(root, 60);
root = insertNode(root, 80);
return 0;
}
Output :
Assigmnet 6
Assignment 7
Write a code for insertion in binary tree.
#include <bits/stdc++.h>
using namespace std;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
// Return new root
return x;
}
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return node;
}
// Driver code
int main() {
Node* root = nullptr;
// Insert keys into the AVL tree
root = insert(root, 9);
root = insert(root, 5);
root = insert(root, 10);
root = insert(root, 0);
root = insert(root, 6);
root = insert(root, 11);
root = insert(root, -1);
root = insert(root, 1);
root = insert(root, 2);
return 0;
}
output :
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return node;
}
if (root == nullptr)
return root;
root->height = 1 + max(height(root->left), height(root->right));
return root;
}
// Driver code
int main() {
Node *root = nullptr;
ouput :
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return node;
}
if (temp == nullptr) {
temp = root;
root = nullptr;
} else
*root = *temp;
free(temp);
} else {
Node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}
if (root == nullptr)
return root;
return root;
}
// Driver code
int main() {
Node* root = nullptr;
return 0;
}
output :