DSAlab
DSAlab
#include <stdio.h>
int main(){
int s = 0;
int arr[11]={0,1,2,3,4,5,6,7,8,9,10};
for (int i=1;i<11;i=i+2){
printf("%d\n"
,arr[i]);
s= s+arr[i];
}
printf("The sum is : %d"
,s);
}
Q-2)Write a c program to add elements from array in even position.
#include <stdio.h>
int main(){
int s = 0;
int arr[11]={0,1,2,3,4,5,6,7,8,9,10};
for (int i=0;i<11;i=i+2){
printf("%d\n"
,arr[i]);
s= s+arr[i];
}
printf("The sum is : %d"
,s);
}
Q-3) Write a c program to print array in reverse order.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original Array: ");
for (int i = 0; i < size; i++) {printf("%d "
, arr[i]);
}
printf("\n");
printf("Reversed Array: ");
for (int i = size - 1; i >= 0; i--) {
printf("%d "
, arr[i]);
}
return 0;
}
Q-4) Write a c program to square the elements of array and print the sum.
#include <stdio.h>
int main() {
int s =0;
int arr[11]={1,2,3,4,5,6,7,8,9,10,11};
for (int i=0;i<11;i++){
s = s+(arr[i]*arr[i]);
}printf("%d\n"
,s);
return 0;
}
Q-5) Write a c program to print the factorial of a given number
#include <stdio.h>
int main() {
int number, factorial = 1;
printf("Enter a non-negative integer: ");
scanf("%d"
, &number);
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
for (int i = 1; i <= number; ++i) {
factorial *= i;
}
printf("Factorial of %d = %d\n"
, number, factorial);
}return 0;
}
Q-6) Write a c program to print Fibonacci series up to a given number
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of iterations for the Fibonacci series: ");
scanf("%d"
, &n);
printf("Fibonacci Series up to %d iterations:\n"
, n);
for (int i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d "
, next);
}
printf("\n");
return 0;
}
Q-7) Write a c program to print numbers in ascending order
#include <stdio.h>
void sortAscending(int arr[], int n) {
int temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}}
}
}
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d"
, &n);
int arr[n];
printf("Enter %d elements:\n"
, n);
for (int i = 0; i < n; i++)
scanf("%d"
, &arr[i]);
sortAscending(arr, n);
printf("Numbers in ascending order: ");
for (int i = 0; i < n; i++)
printf("%d "
, arr[i]);
return 0;
}
Q-8)Write a c program to print numbers in descending order.
#include <stdio.h>
void sortDescending(int arr[], int n) {
int temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d"
, &n);
int arr[n];
printf("Enter %d elements:\n"
, n);
for (int i = 0; i < n; i++)
scanf("%d"
, &arr[i]);
sortDescending(arr, n);
printf("Numbers in descending order: ");
for (int i = 0; i < n; i++)printf("%d "
, arr[i]);
return 0;
}
Q-9) Write a c program to print the elements from odd position
#include <stdio.h>
int main(){
int arr[11]={0,1,2,3,4,5,6,7,8,9,10};
for (int i=1;i<11;i=i+2){
printf("%d\n"
,arr[i]);
}
}
Q-10) Write a c program to print the elements from even position
#include <stdio.h>
int main(){
int arr[11]={0,1,2,3,4,5,6,7,8,9,10};
for (int i=0;i<11;i=i+2){
printf("%d\n"
,arr[i]);
}
}
Q-11)Write a c Program to read the information about book and print.
#include <stdio.h>struct Book {
char title[100];
char author[100];
int year;
float price;
};
int main() {
struct Book book;
printf("Enter information about the book:\n");
printf("Title: ");
scanf(" %[^\n]", book.title);
printf("Author: ");
scanf(" %[^\n]", book.author);
printf("Year of publication: ");
scanf("%d"
, &book.year);
printf("Price: ");
scanf("%f"
, &book.price);
printf("\nBook Information:\n");
printf("Title: %s\n"
, book.title);
printf("Author: %s\n"
, book.author);
printf("Year of publication: %d\n"
, book.year);
printf("Price: $%.2f\n"
, book.price);
return 0;
}
Q-12) write a c program to read the information about 10 books.
#include <stdio.h>
struct Book {
char title[100];
char author[100];
int year;
float price;
};
int main() {
struct Book books[10];
printf("Enter information about 10 books:\n");for (int i = 0; i < 10; i++) {
printf("\nBook %d:\n", i + 1);
printf("Title: ");
scanf(" %[^\n]", books[i].title);
printf("Author: ");
scanf(" %[^\n]", books[i].author);
printf("Year of publication: ");
scanf("%d"
, &books[i].year);
printf("Price: ");
scanf("%f"
, &books[i].price);
}
printf("\nBook Information:\n");
for (int i = 0; i < 10; i++) {
printf("\nBook %d:\n", i + 1);
printf("Title: %s\n"
, books[i].title);
printf("Author: %s\n"
, books[i].author);
printf("Year of publication: %d\n"
, books[i].year);
printf("Price: $%.2f\n"
, books[i].price);
}
return 0;
}
Q-13)write a c program to read array of 10 integer and print the sum
using pointer variable
#include <stdio.h>
int main() {
int arr[10];
int *ptr = arr;
int sum = 0;
printf("Enter 10 integers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", ptr + i);
}
for (int i = 0; i < 10; i++) {
sum += *(ptr + i);
}
printf("Sum of the elements: %d\n", sum);
return 0;
}
Q-14) Write a c program to calculate the Net Salary of an employee
using structures Basic-5000, DA-50%, HRA-12%
#include <stdio.h>
struct Employee {
char name[50];
int basicSalary;
};
float calculateNetSalary(struct Employee emp) {
float DA_percentage = 0.5;
float HRA_percentage = 0.12;
float DA = emp.basicSalary * DA_percentage;
float HRA = emp.basicSalary * HRA_percentage;
float netSalary = emp.basicSalary + DA + HRA;
return netSalary;
}
int main() {
struct Employee emp;
printf("Enter employee name: ");
scanf(" %[^\n]", emp.name);
printf("Enter basic salary: ");
scanf("%d"
, &emp.basicSalary);
float netSalary = calculateNetSalary(emp);printf("\nEmployee Details:\n");
printf("Name: %s\n"
, emp.name);
printf("Basic Salary: %d\n"
, emp.basicSalary);
printf("Net Salary: %.2f\n", netSalary);
return 0;
}
Q-15) Write a c program to calculate the electricity bill for the customer
using structures
using meter reading.
Bill Amount = unites slab
If unites <100-0/-
Unites> 100 and <=200-2/-
Unites 200 and <=300-4/-
Unites >300-5/-
#include <stdio.h>
int main(){
struct bill{
int c_no;
int units;
int cost;
}b1;
b1.c_no = 1;
printf("Enter the number of units consumed: ");
scanf("%d"
,&b1.units);
if (b1.units<100){
b1.cost = 0 ;
}
else if (b1.units>=100 && b1.units<200){
b1.cost = b1.units*2;
}
else if(b1.units>=200 && b1.units<300){
b1.cost =200*2+(b1.units-200)*4;
}
else{b1.cost= (200*2)+(100*4)+(b1.units-300)*5;
}
printf("Customer number: %d\n"
,b1.c_no++);
printf("Total units consumed: %d\n"
,b1.units);
printf("The total b1.cost is: %d\n"
,b1.cost);
return 0;
}
Q-16)Write a c program to calculate net salary for 10 employees.
#include <stdio.h>
struct Employee {
char name[50];
int basicSalary;
};
float calculateNetSalary(struct Employee emp) {
float DA_percentage = 0.5;
float HRA_percentage = 0.12;
float DA = emp.basicSalary * DA_percentage;
float HRA = emp.basicSalary * HRA_percentage;
float netSalary = emp.basicSalary + DA + HRA;
return netSalary;
}
int main() {
struct Employee emp;
int n;
printf("Enter the number of employees: ");
scanf("%d"
,&n);
for(int i=0 ;i<n;i++){
printf("Enter employee name: ");
scanf(" %[^\n]", emp.name);
printf("Enter basic salary: ");
scanf("%d"
, &emp.basicSalary);
float netSalary = calculateNetSalary(emp);
printf("\nEmployee Details:\n");
printf("Name: %s\n"
, emp.name);
printf("Basic Salary: %d\n"
, emp.basicSalary);
printf("Net Salary: %.2f\n\n", netSalary);
}
return 0;
}
Q-17) Write a c program to generate the electricity bill for 10 customers
#include <stdio.h>
int main(){
struct bill{int c_no;
int units;
int cost;
}b1;
b1.c_no = 1;
for (int i=0;i<10;i++){
printf("Enter the number of units consumed: ");
scanf("%d"
,&b1.units);
if (b1.units<100){
b1.cost = 0 ;
}
else if (b1.units>=100 && b1.units<200){
b1.cost = b1.units*2;
}
else if(b1.units>=200 && b1.units<300){
b1.cost =200*2+(b1.units-200)*4;
}
else{
b1.cost= (200*2)+(100*4)+(b1.units-300)*5;
}
printf("Customer number: %d\n"
,b1.c_no++);
printf("Total units consumed: %d\n"
,b1.units);
printf("The total b1.cost is: %d\n"
,b1.cost);
}
return 0;
}
#include <stdio.h>
#define MAX_SIZE 10
int main() {
int stack[MAX_SIZE];
int top;
int choice;
int value;
initialize(stack, &top);
while (1) {
printf("\nStack Menu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Print Stack\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(stack, &top, value);
break;
case 2:
pop(stack, &top);
break;
case 3:
printStack(stack, top);
break;
case 4:
printf("Exiting program\n");
return 0;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}
2. Write a C Program to implement Stack using linked
lists.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* top = NULL;
int choice;
int value;
while (1) {
printf("\nStack Menu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Print Stack\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&top, value);
break;
case 2:
pop(&top);
break;
case 3:
printStack(top);
break;
case 4:
printf("Exiting program\n");
return 0;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}
3. Write a C Program to implement Queue using arrays.
#include <stdio.h>
#define MAX_SIZE 10
int isFull() {
return ((rear + 1) % MAX_SIZE == front);
}
int isEmpty() {
return (front == -1 && rear == -1);
}
void dequeue() {
if (isEmpty()) {
printf("Queue underflow! Cannot dequeue from an empty queue\n");
return;
}
int item = queue[front];
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
printf("%d dequeued from the queue\n", item);
}
void printQueue() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}
printf("Queue elements: ");
int i = front;
do {
printf("%d ", queue[i]);
i = (i + 1) % MAX_SIZE;
} while (i != (rear + 1) % MAX_SIZE);
printf("\n");
}
int main() {
int choice;
int item;
while (1) {
printf("\nQueue Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Print Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &item);
enqueue(item);
break;
case 2:
dequeue();
break;
case 3:
printQueue();
break;
case 4:
printf("Exiting program\n");
return 0;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
return 0;
}
4. Write a C Program to implement Queue using linked
list.
#include <stdio.h>
Node* initializeQueue() {
return NULL;
}
if (isEmpty(rear)) {
return newNode;
} else {
rear->next = newNode;
return newNode;
}
}
return front;
}
printf("Queue: ");
while (front != NULL) {
printf("%d ", front->data);
front = front->next;
}
printf("\n");
}
int main() {
Node* front = initializeQueue();
Node* rear = front;
displayQueue(front);
front = dequeue(front);
displayQueue(front);
return 0;
}
Node* initializeList() {
return NULL;
}
if (head == NULL) {
return newNode;
}
temp->next = newNode;
return head;
}
return head;
}
Node* deleteAtEnd(Node* head) {
if (head == NULL) {
printf("List is empty. Cannot delete from the end.\n");
return head;
}
if (head->next == NULL) {
free(head);
return NULL;
}
free(temp->next);
temp->next = NULL;
return head;
}
if (position == 1) {
Node* temp = head;
head = head->next;
free(temp);
return head;
}
return head;
}
void displayList(Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
printf("List: ");
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
Node* head = initializeList();
displayList(head);
head = deleteAtBeginning(head);
displayList(head);
head = deleteAtEnd(head);
displayList(head);
int positionToDelete = 3;
head = deleteAtPosition(head, positionToDelete);
displayList(head);
return 0;
}
6. Write a C Program to perform polynomial using
linked list for the following polunomials:
i. Y1=x^3+21x^2+10x+5
ii. Y1=x^4+3x^2=2x+5
#include <stdio.h>
newNode->next = temp->next;
temp->next = newNode;
return head;
}
if (exp1 == exp2) {
poly1 = poly1->next;
poly2 = poly2->next;
} else if (exp1 > exp2) {
poly1 = poly1->next;
} else {
poly2 = poly2->next;
}
}
return result;
}
if (exp1 == exp2) {
poly1 = poly1->next;
poly2 = poly2->next;
} else if (exp1 > exp2) {
poly1 = poly1->next;
} else {
poly2 = poly2->next;
}
}
return result;
}
poly1 = poly1->next;
}
return result;
}
return result;
}
printf("Polynomial: ");
while (head != NULL) {
printf("%dx^%d ", head->coefficient, head->exponent);
if (head->next != NULL) {
printf("+ ");
}
head = head->next;
}
printf("\n");
}
int main() {
Node* poly1 = NULL;
Node* poly2 = NULL;
poly1 = insertTerm(poly1, 5, 3);
poly1 = insertTerm(poly1, 21, 2);
poly1 = insertTerm(poly1, 10, 1);
poly1 = insertTerm(poly1, 5, 0);
displayPolynomial(poly1);
displayPolynomial(poly2);
return 0;
}
Node* initializeList() {
return NULL;
}
if (head == NULL) {
return newNode;
}
newNode->next = head;
head->prev = newNode;
return newNode;
}
if (position <= 0) {
printf("Invalid position. Cannot insert at position %d.\n", position);
return head;
}
if (position == 1) {
newNode->next = head;
head->prev = newNode;
return newNode;
}
if (temp == NULL) {
printf("Invalid position. Cannot insert at position %d.\n", position);
return head;
}
newNode->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
newNode->prev = temp;
temp->next = newNode;
return head;
}
if (head->next == NULL) {
free(head);
return NULL;
}
temp->prev->next = NULL;
free(temp);
return head;
}
if (position <= 0) {
printf("Invalid position. Cannot delete at position %d.\n", position);
return head;
}
if (position == 1) {
Node* temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);
return head;
}
if (temp == NULL) {
printf("Invalid position. Cannot delete at position %d.\n", position);
return head;
}
temp->prev->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
return head;
}
int main() {
Node* head = initializeList();
displayList(head);
head = deleteAtEnd(head);
displayList(head);
return 0;
}
return result;
}
int main() {
Complex num1, num2, result;
return 0;
}
Radix sort
#include <stdio.h>
// Change count[i] so that count[i] contains actual position of this digit in output[]
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
// Copy the output array to arr[], so that arr[] now contains sorted numbers
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
// Do counting sort for every digit. Note that instead of passing digit number, exp is passed. exp is 10^i where i is the
current digit number
for (int exp = 1; max / exp > 0; exp *= 10)
countingSort(arr, n, exp);
}
// Example usage
int main() {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
radixSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Quick sort
#include <stdio.h>
// Example usage
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Shell sort
// C++ implementation of Shell Sort
#include <iostream>
using namespace std;
int main()
{
int arr[] = {12, 34, 54, 2, 3}, i;
int n = sizeof(arr)/sizeof(arr[0]);
shellSort(arr, n);
cout << "\nArray after sorting: \n";
printArray(arr, n);
return 0;
}
Q1) Write a c program to implement simple hash table using arrays with
following keys.
0, 1, 4, 16, 9
The hash function is f(x) = x mod 10;
Ans) #include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct Node {
int key;
int value;
struct Node *next;
};
if (hashTable[index] == NULL) {
hashTable[index] = newNode;
} else {
struct Node *current = hashTable[index];
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
int main() {
for (int i = 0; i < SIZE; i++) {
hashTable[i] = NULL;
}
insert(0, 10);
insert(1, 20);
insert(4, 30);
insert(16, 40);
insert(9, 50);
return 0;
}
Output:
Key 0 found with value 10
Key 1 found with value 20
Key 4 found with value 30
Key 16 found with value 40
Key 9 found with value 50
Q2) Write a c program to implement simple hash table using arrays with
following keys.0, 1, 4, 9, 16, 25, 36, 49, 64, 81The hash function is f(x) = x
mod 10;
Ans)
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct Node{
int key;
int value;
struct Node *next;
};
if (hashTable[index]==NULL) {
hashTable[index]=newNode;
} else {
struct Node *current=hashTable[index];
while(current->next!=NULL){
current=current->next;
}
current->next=newNode;
}
}
int main() {
for (int i=0;i<SIZE;i++) {
hashTable[i]=NULL;
}
int keys[]={0,1,4,9,16,25,36,49,64,81};
for (int i=0;i<sizeof(keys)/sizeof(keys[0]);i++) {
insert(keys[i],keys[i]*keys[i]);
}
for (int i=0;i<sizeof(keys)/sizeof(keys[0]);i++){
int key=keys[i];
int value=search(key);
if (value!=-1){
printf("Key %d found with value %d\n", key, value);
}
else{
printf("Key %d not found\n", key);
}
}
return 0;
}
Output:
Key 0 found with value 0
Key 1 found with value 1
Key 4 found with value 16
Key 9 found with value 81
Key 16 found with value 256
Key 25 found with value 625
Key 36 found with value 1296
Key 49 found with value 2401
Key 64 found with value 4096
Key 81 found with value 6561
Q3) Write a c program to implement heap sort for sorting the following
numbers 2, 5, 3, 7, 4, 9, 20, 30, 45, 50.By generating min
heap and max heap.
Ans)
#include <stdio.h>
if (largest != i) {
swap(&arr[i], &arr[largest]);
maxHeapify(arr, n, largest);
}
}
if (smallest != i) {
swap(&arr[i], &arr[smallest]);
minHeapify(arr, n, smallest);
}
}
int main() {
int arr[] = {2, 5, 3, 7, 4, 9, 20, 30, 45, 50};
int n = sizeof(arr) / sizeof(arr[0]);
heapSortMaxHeap(arr, n);
printf("Sorted array using max heap:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
heapSortMinHeap(arr, n);
printf("Sorted array using min heap:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
Sorted array using max heap:
2 3 4 5 7 9 20 30 45 50
Sorted array using min heap:
50 45 30 20 9 7 5 4 3 2