0% found this document useful (0 votes)
15 views

Gaurav Kumar Dsa File

The document contains multiple C++ programming tasks, including reversing a positive integer, finding the greatest of three numbers, identifying common elements in arrays, calculating sums using recursion, checking for palindromes, and manipulating linked lists. Each task is accompanied by code snippets demonstrating the implementation of the respective algorithms. Additionally, there are examples of stack manipulation using recursion to reverse a stack without loops.

Uploaded by

greatestdemon70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Gaurav Kumar Dsa File

The document contains multiple C++ programming tasks, including reversing a positive integer, finding the greatest of three numbers, identifying common elements in arrays, calculating sums using recursion, checking for palindromes, and manipulating linked lists. Each task is accompanied by code snippets demonstrating the implementation of the respective algorithms. Additionally, there are examples of stack manipulation using recursion to reverse a stack without loops.

Uploaded by

greatestdemon70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

Write a C++ program to reverse a given positive integer number


and display the result
#include <iostream>
using namespace std;
int main() {
int num, reversedNum = 0;
// Input from the user
cout << "Enter a positive integer: ";
cin >> num;
// Reversing the number
while (num > 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
cout << "Reversed number: " << reversedNum << endl;
return 0;
}

2.Write a C/ C++ program to find the greatest of three numbers.


#include <iostream>

using namespace std;

int main() {

int num1, num2, num3;

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

int greatest;

if (num1 >= num2 && num1 >= num3)


greatest = num1;

else if (num2 >= num1 && num2 >= num3)

greatest = num2;

else

greatest = num3;

cout << "The greatest number is: " << greatest << endl;

return 0;

3.Consider two single dimensional arrays of size 20 and 30


respectively. Write a program in C ++ to find out the elements which
are common in both arrays

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
const int size1 = 20, size2 = 30;
int arr1[size1], arr2[size2];
// Taking input for the first array
cout << "Enter 20 elements for the first array: ";
for (int i = 0; i < size1; i++) {
cin >> arr1[i];
}
// Taking input for the second array
cout << "Enter 30 elements for the second array: ";
for (int i = 0; i < size2; i++) {
cin >> arr2[i];
}
unordered_set<int> set1(arr1, arr1 + size1);
unordered_set<int> commonElements;
// Finding common elements
for (int i = 0; i < size2; i++) {
if (set1.find(arr2[i]) != set1.end()) {
commonElements.insert(arr2[i]);
}
}
// Displaying common elements
cout << "Common elements are: ";
for (int element : commonElements) {
cout << element << " ";
}
cout << endl;
return 0;
}

4.Given an array of integers, find the sum of array elements using


recursion.
#include <iostream>
using namespace std;
// Recursive function to find the sum of array elements
int sumArray(int arr[], int size) {
if (size == 0) return 0;
return arr[size - 1] + sumArray(arr, size - 1);
}
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int arr[size];
cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
cout << "Sum of array elements: " << sumArray(arr, size) << endl;
return 0;
}

5.Given a number, find the sum of its digits using recursion.


#include <iostream>
using namespace std;
//Calculate sum of digits
int sumOfDigits(int num) {
if (num == 0) // Base case
return 0;
return (num % 10) + sumOfDigits(num / 10); // Recursive step
}
int main() {
int num;
// Input from user
cout << "Enter a number: ";
cin >> num;
cout << "Sum of digits: " << sumOfDigits(num) << endl;
return 0;
}

6.Given a string, write a recursive function that checks if the


given string is a palindrome, else, not a palindrome.
#include <iostream>
using namespace std;
//Check if a string is a palindrome
bool isPalindrome(string str, int start, int end) {
if (start >= end)
return true;
if (str[start] != str[end])
return false;
return isPalindrome(str, start + 1, end - 1);
}
int main() {
string str;
// Input from user
cout << "Enter a string: ";
cin >> str;
if (isPalindrome(str, 0, str.length() - 1))
cout << "The string is a palindrome." << endl;
else
cout << "The string is not a palindrome." << endl;
return 0;

7.Given an array, arr, of integers, your task is to return the smallest and
the second smallest elements in the array. If the smallest and the
second smallest elements do not exist, return -1.
#include <iostream>
#include <limits.h>
using namespace std;
void findSmallestAndSecondSmallest(int arr[], int size) {
if (size < 2) {
cout << "-1" << endl;
return;
}
int smallest = INT_MAX, secondSmallest = INT_MAX;
for (int i = 0; i < size; i++) {
if (arr[i] < smallest) {
secondSmallest = smallest;
smallest = arr[i];
} else if (arr[i] > smallest && arr[i] < secondSmallest) {
secondSmallest = arr[i];
}
}
if (secondSmallest == INT_MAX)
cout << "-1" << endl;
else
cout << "Smallest: " << smallest << ", Second Smallest: " << secondSmallest << endl;
}
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int arr[size];
cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
findSmallestAndSecondSmallest(arr, size);
return 0;
}

8.Write a C++ program to print the elements of a 2D array using


pointers. Given the following 2D array:
arr[3][4] = {{2, 3, 4}, {5, 6, 7}}

Use a pointer to print each element of the array.

#include <iostream>
using namespace std;
int main() {
int arr[2][3] = {{2, 3, 4}, {5, 6, 7}};
int *ptr = &arr[0][0]; // Pointer to the first element of the array
cout << "Elements of the 2D array using pointers:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << *(ptr + i * 3 + j) << " ";
}
cout << endl;
}
return 0;
}

9.Write a C++ program to insert a node at both the beginning


and end of a linked list and delete a node from both the
beginning and end of a linked list.

#include <iostream>
using namespace std;

// Node structure for the linked list


struct Node {
int data;
Node* next;
};

// Function to create a new node


Node* createNode(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = nullptr;
return newNode;
}

// Insert a node at the beginning of the list


void insertAtBeginning(Node** head, int data) {
Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
cout << "Inserted " << data << " at the beginning" << endl;
}

// Insert a node at the end of the list


void insertAtEnd(Node** head, int data) {
Node* newNode = createNode(data);

// If list is empty, make new node as head


if (*head == nullptr) {
*head = newNode;
} else {
// Traverse to the last node
Node* temp = *head;
while (temp->next != nullptr) {
temp = temp->next;
}
// Link the last node to new node
temp->next = newNode;
}
cout << "Inserted " << data << " at the end" << endl;
}

// Delete the first node from the list


void deleteFromBeginning(Node** head) {
// Check if list is empty
if (*head == nullptr) {
cout << "List is empty, cannot delete from beginning" << endl;
return;
}

// Store the current head in temporary pointer


Node* temp = *head;

// Move head to next node


*head = (*head)->next;

cout << "Deleted " << temp->data << " from the beginning" <<
endl;

// Free memory of the deleted node


delete temp;
}

// Delete the last node from the list


void deleteFromEnd(Node** head) {
// Check if list is empty
if (*head == nullptr) {
cout << "List is empty, cannot delete from end" << endl;
return;
}

// If there's only one node


if ((*head)->next == nullptr) {
cout << "Deleted " << (*head)->data << " from the end" << endl;
delete *head;
*head = nullptr;
return;
}
// Traverse to the second last node
Node* temp = *head;
while (temp->next->next != nullptr) {
temp = temp->next;
}

// Store the last node


Node* lastNode = temp->next;
cout << "Deleted " << lastNode->data << " from the end" << endl;

// Remove the link to last node


temp->next = nullptr;

// Free memory of the last node


delete lastNode;
}

// Display all nodes in the list


void displayList(Node* head) {
if (head == nullptr) {
cout << "List is empty" << endl;
return;
}

cout << "Current list: ";


Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// Function to free all nodes in the list
void freeList(Node** head) {
while (*head != nullptr) {
Node* temp = *head;
*head = (*head)->next;
delete temp;
}
}

int main() {
Node* head = nullptr; // Initialize empty list

cout << "=== Linked List Operations Demo (Using Struct Only)
===" << endl;

// Initial display
displayList(head);

cout << "\n--- Insertion Operations ---" << endl;

// Insert at beginning
insertAtBeginning(&head, 10);
displayList(head);

insertAtBeginning(&head, 20);
displayList(head);

// Insert at end
insertAtEnd(&head, 30);
displayList(head);

insertAtEnd(&head, 40);
displayList(head);
// More insertions to demonstrate
insertAtBeginning(&head, 5);
insertAtEnd(&head, 50);
displayList(head);

cout << "\n--- Deletion Operations ---" << endl;

// Delete from beginning


deleteFromBeginning(&head);
displayList(head);

// Delete from end


deleteFromEnd(&head);
displayList(head);

// More deletions
deleteFromBeginning(&head);
deleteFromEnd(&head);
displayList(head);

// Delete remaining nodes


deleteFromBeginning(&head);
deleteFromEnd(&head);
displayList(head);

// Try to delete from empty list


deleteFromBeginning(&head);
deleteFromEnd(&head);

// Clean up any remaining memory


freeList(&head);
return 0;
}

10.Write a program to reverse a stack using recursion, without


using any loop.
#include <iostream>
#include <stack>
using namespace std;
// Function to insert an element at the bottom of a stack
void insertAtBottom(stack<int> &st, int val) {
if (st.empty()) {
st.push(val);
return;
}
int top = st.top();
st.pop();
insertAtBottom(st, val);
st.push(top);
}
// Function to reverse the stack using recursion
void reverseStack(stack<int> &st) {
if (st.empty()) return;
int top = st.top();
st.pop();
reverseStack(st);
insertAtBottom(st, top);
}
// Function to print the stack
void printStack(stack<int> st) {
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
cout << endl;
}
int main() {
stack<int> st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
cout << "Original Stack: ";
printStack(st);
reverseStack(st);
cout << "Reversed Stack: ";
printStack(st);
return 0;
}
NAME: GAURAV KUMAR
ROLL NO: 2024UCM2380

You might also like