SC Lab File
SC Lab File
(Gurugram)
Professor Signature
Table of Content
Sr. Questions
No.
1 Write a program to find the Armstrong numbers between two given intervals.
2 Implement a program to convert a decimal number into binary, octal, and hex.
3 Write a program to find the sum of diagonal elements of a square matrix.
5 Implement a program to merge two sorted arrays into a single sorted array.
8 Write a program to find the second largest element in an array without sorting.
15 Write a program to create a binary search tree (BST) and perform in-ordertraversal.
16 Create a program to implement Dijkstra’s algorithm for shortest path in a graph.
17 Write a program to implement bubble sort, selection sort, insertion sort, andcompare their
performance.
18 Develop a program to implement DFS (depth-first search) on a graph.
EXPERIMENT 1
Q Write a program to find the Armstrong numbers between two given intervals.
#include <iostream>
int main() {
int lower, upper;
cout << "Armstrong numbers between " << lower << " and " << upper << " are:" << endl;
OUTPUT
EXPERIMENT 2
Q Implement a program to convert a decimal number into binary, octal, and hex.
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int decimal;
if (decimal < 0) {
cout << "Please enter a non-negative number." << endl;
return 1;
}
// Convert and display the results
cout << "Binary: " << decimalToBinary(decimal) << endl;
cout << "Octal: " << decimalToOctal(decimal) << endl;
cout << "Hexadecimal: " << decimalToHexadecimal(decimal) << endl;
return 0;
}
OUTPUT
EXPERIMENT 3
Q Write a program to find the sum of diagonal elements of a square matrix.
#include <iostream>
using namespace std;
int main() {
int n;
int diagonalSum = 0;
// If the matrix size is odd, we need to subtract the middle element (because it was counted twice)
if (n % 2 != 0) {
diagonalSum -= matrix[n / 2][n / 2];
}
return 0;
}
OUTPUT
EXPERIMENT 4
Q Develop a program to transpose a matrix without using an additional matrix
#include <iostream>
using namespace std;
int main() {
int n;
return 0;
}
EXPERIMENT 5
Q Implement a program to merge two sorted arrays into a single sorted array.
#include <iostream>
using namespace std;
int main() {
int n1, n2;
return 0;
}
EXPERIMENT 6
Q Write a program to check whether two strings are anagrams.
#include <iostream>
#include <algorithm> // For sorting the strings
using namespace std;
int main() {
string str1, str2;
return 0;
}
EXPERIMENT 7
Q Develop a program to implement the Sieve of Eratosthenes for prime numbers.
#include <iostream>
#include <vector>
using namespace std;
void sieveOfEratosthenes(int n) {
// Create a boolean array "prime[0..n]" and initialize all entries as true.
vector<bool> prime(n + 1, true);
int main() {
int n;
// Input: Limit up to which we need to find primes
cout << "Enter the limit for finding prime numbers: ";
cin >> n;
return 0;
}
OUTPUT
EXPERIMENT 8
Q Write a program to find the second largest element in an array without sorting.
#include <iostream>
#include <climits> // For INT_MIN
using namespace std;
int main() {
int n;
int arr[n];
if (secondLargest != -1) {
cout << "The second largest element is: " << secondLargest << endl;
}
return 0;
}
OUTPUT
EXPERIMENT 9
Q Create a program to check if a string is a valid palindrome (remove non-
alphanumeric characters).
#include <iostream>
#include <cctype> // For isalnum and tolower functions
using namespace std;
int main() {
string input;
return 0;
}
OUTPUT
EXPERIMENT 10
Q Write a program to implement binary search on a sorted array.
#include <iostream>
using namespace std;
int main() {
int n, key;
int arr[n];
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
return 0;
}
OUTPUT
EXPERIMENT 11
QWrite a program to implement a stack using an array (push, pop, peek).
#include<iostream>
using namespace std;
class stack{
//stack implementation through array
public:
int *arr;
int top;
int size;
stack(int size){
this->size=size;
arr=new int [size];
top=-1;
}
void pop(){
if(top>=0){
top--;
}
else
cout<<" empty stack "<<endl;
}
bool empty(){
return top==-1;
}
int Top(){
if(!empty()){
return arr[top];
}
else{
cout<<" empty stack "<<endl;
}
return -1;
}
int findmiddle(){
int n =top/2;
cout<<"mid is at nth position and is "<<arr[n];
return n;
}
};
int main(){
stack s(3);
s.push(1);
s.push(2);
s.push(3);
// s.push(4);
// s.push(5);
// s.push(6);
// s.push(7);
// s.push(8);
#include <iostream>
using namespace std;
public:
// Constructor to initialize the queue
Queue() {
front = nullptr;
rear = nullptr;
}
return 0;
}
OUTPUT
EXPERIMENT 13
Q Write a program to perform BFS (breadth-first search) on a graph (adjacency
matrix).
#include <iostream>
#include <queue> // for using queue
#include <vector> // for using vector
using namespace std;
cout << "BFS traversal starting from node " << startNode << ": ";
while (!q.empty()) {
// Dequeue a node from the front
int currentNode = q.front();
q.pop();
return 0;
}
EXPERIMENT 14
Q Develop a program to reverse a linked list both iteratively and recursively.
#include <iostream>
using namespace std;
public:
// Constructor initializes the list as empty
LinkedList() {
head = nullptr;
}
// Main function
int main() {
LinkedList list;
return 0;
}
EXPERIMENT 15
Q Write a program to create a binary search tree (BST) and perform in-order
traversal.
#include <iostream>
using namespace std;
// Main function
int main() {
Node* root = nullptr; // Initialize the root of the BST as null
// Insert nodes into the BST
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
return 0;
}
OUTPUT
EXPERIMENT 16
Q Create a program to implement Dijkstra’s algorithm for shortest path in a graph.
#include <iostream>
#include <vector>
#include <queue>
#include <climits> // For INT_MAX
using namespace std;
while (!pq.empty()) {
int current_dist = pq.top().first; // Distance of current vertex
int current_vertex = pq.top().second;
pq.pop();
return 0;
}
OUTPUT
EXPERIMENT 17
Q Write a program to implement bubble sort, selection sort, insertion sort, and
compare their performance.
#include <iostream>
#include <vector>
#include <chrono> // For measuring execution time
using namespace std;
// Bubble Sort
void bubbleSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}}}}
// Selection Sort
void selectionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
}
// Insertion Sort
void insertionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
// Selection Sort
vector<int> selectionArr = arr;
auto startSelection = chrono::high_resolution_clock::now();
selectionSort(selectionArr);
auto endSelection = chrono::high_resolution_clock::now();
auto durationSelection = chrono::duration_cast<chrono::microseconds>(endSelection -
startSelection).count();
cout << "Selection Sort:\nSorted Array: ";
displayArray(selectionArr);
cout << "Execution Time: " << durationSelection << " microseconds\n\n";
// Insertion Sort
vector<int> insertionArr = arr;
auto startInsertion = chrono::high_resolution_clock::now();
insertionSort(insertionArr);
auto endInsertion = chrono::high_resolution_clock::now();
auto durationInsertion = chrono::duration_cast<chrono::microseconds>(endInsertion -
startInsertion).count();
cout << "Insertion Sort:\nSorted Array: ";
displayArray(insertionArr);
cout << "Execution Time: " << durationInsertion << " microseconds\n";
return 0;}
OUTPUT
EXPERIMENT 18
Q Develop a program to implement DFS (depth-first search) on a graph.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int vertices, edges;
cout << "Enter the number of vertices and edges: ";
cin >> vertices >> edges;
cout << "Enter the edges (u, v): " << endl;
for (int i = 0; i < edges; i++) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u); // Comment this line if the graph is directed
}
int startNode;
cout << "Enter the starting node for DFS: ";
cin >> startNode;
cout << "DFS Traversal starting from node " << startNode << ": ";
dfs(startNode, graph, visited);
cout << endl;
return 0;
}