Posts

Showing posts from May, 2024

Write a c program to demonstrate pre-processor directives conditional Compilation

 #include <stdio.h> // Define DEBUG to enable debug-specific code #define DEBUG // Define VERSION to switch between different versions of the code #define VERSION 2 int main() {     printf("Conditional Compilation Demonstration\n"); #ifdef DEBUG     printf("Debug mode is enabled.\n"); #endif #if VERSION == 1     printf("This is version 1 of the program.\n"); #elif VERSION == 2     printf("This is version 2 of the program.\n"); #else     printf("Unknown version.\n"); #endif #ifndef DEBUG     printf("Debug mode is not enabled.\n"); #endif     return 0; }

Write a c program to Create a Circular DoubleLinked list and perform Following Operations A. Insertion After a particular node B. Insertion Before a particular node C. Search a particular node D. Return a particular node E. Deletion before a particular node F. Delete a particular node

 #include <stdio.h> #include <stdlib.h> // Define the structure for a node in the circular doubly linked list struct Node {     int data;     struct Node* next;     struct Node* prev; }; // Function to create a new node with given data struct Node* createNode(int data) {     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));     newNode->data = data;     newNode->next = newNode;     newNode->prev = newNode;     return newNode; } // Function to insert a node at the end of the circular doubly linked list void insertAtEnd(struct Node** head, int data) {     struct Node* newNode = createNode(data);     if (*head == NULL) {         *head = newNode;     } else {         struct Node* last = (*head)->prev;         newNode->next = *head;         (*head)->prev ...

Write a c program to Create a Circular single Linked list and perform Following Operations A. Insertion After a particular node B. Insertion Before a particular node C. Search a particular node D. Return a particular node E. Deletion before a particular node F. Delete a particular node

 #include <stdio.h> #include <stdlib.h> // Define the structure for a node in the circular linked list struct Node {     int data;     struct Node* next; }; // Function to create a new node with given data struct Node* createNode(int data) {     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));     newNode->data = data;     newNode->next = NULL;     return newNode; } // Function to insert a node at the beginning of the circular linked list void insertAtBeginning(struct Node** head, int data) {     struct Node* newNode = createNode(data);     if (*head == NULL) {         newNode->next = newNode;         *head = newNode;     } else {         struct Node* temp = *head;         while (temp->next != *head) {             temp = temp->next;   ...

Write a c program to Create a Circular Linked list and perform Following Operations A. Insertion At Beginning B. Insertion At End C. Insertion After a particular node Insertion Before a particular node E. Insertion at specific position F. Search a particular node G. Return a particular node H. Deletion at the beginning I. Deletion at the end J. Deletion after a particular node K. Deletion before a particular node L. Delete a particular node M. Deletion at a specific position

#include <stdio.h> #include <stdlib.h> // Define the structure for a node in the linked list struct Node {     int data;     struct Node* next; }; // Function to create a new node with given data struct Node* createNode(int data) {     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));     newNode->data = data;     newNode->next = NULL;     return newNode; } // Function to insert a node at the beginning of the linked list void insertAtBeginning(struct Node** head, int data) {     struct Node* newNode = createNode(data);     newNode->next = *head;     *head = newNode; } // Function to insert a node at the end of the linked list void insertAtEnd(struct Node** head, int data) {     struct Node* newNode = createNode(data);     if (*head == NULL) {         *head = newNode;         return;     } ...

Write a c program to check whether the created linked list is palindrome or not

 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> // Define the structure for a node in the linked list struct Node {     int data;     struct Node* next; }; // Function to create a new node with given data struct Node* createNode(int data) {     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));     newNode->data = data;     newNode->next = NULL;     return newNode; } // Function to insert a node at the end of the linked list void insertAtEnd(struct Node** head, int data) {     struct Node* newNode = createNode(data);     if (*head == NULL) {         *head = newNode;         return;     }     struct Node* temp = *head;     while (temp->next != NULL) {         temp = temp->next;     }     temp->next = newNode; } // Function to print the ...

Write a program to Reverse a singly Linked list

 #include <stdio.h> #include <stdlib.h> // Define the structure for a node in the linked list struct Node {     int data;     struct Node* next; }; // Function to create a new node with given data struct Node* createNode(int data) {     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));     newNode->data = data;     newNode->next = NULL;     return newNode; } // Function to insert a node at the end of the linked list void insertAtEnd(struct Node** head, int data) {     struct Node* newNode = createNode(data);     if (*head == NULL) {         *head = newNode;         return;     }     struct Node* temp = *head;     while (temp->next != NULL) {         temp = temp->next;     }     temp->next = newNode; } // Function to print the linked list void printList(...

Write a c program to Create a single Linked list and perform Following Operations A. Insertion At Beginning B. Insertion At End C. Insertion After a particular node D. Insertion Before a particular node E. Insertion at specific position F. Search a particular node G. Return a particular node H. Deletion at the beginning I. Deletion at the end J. Deletion after a particular node K. Deletion before a particular node L. Delete a particular node M. Deletion at a specific position

 #include <stdio.h> #include <stdlib.h> // Define the structure for a node in the linked list struct Node {     int data;     struct Node* next; }; // Function to create a new node with given data struct Node* createNode(int data) {     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));     newNode->data = data;     newNode->next = NULL;     return newNode; } // Function to insert a node at the beginning of the linked list void insertAtBeginning(struct Node** head, int data) {     struct Node* newNode = createNode(data);     newNode->next = *head;     *head = newNode; } // Function to insert a node at the end of the linked list void insertAtEnd(struct Node** head, int data) {     struct Node* newNode = createNode(data);     if (*head == NULL) {         *head = newNode;         return;     ...

Write a c program to perform binary search

 #include <stdio.h> // Function to perform binary search int binarySearch(int arr[], int left, int right, int target) {     while (left <= right) {         int mid = left + (right - left) / 2;         // Check if the target is present at mid         if (arr[mid] == target) {             return mid;         }         // If target is greater, ignore the left half         if (arr[mid] < target) {             left = mid + 1;         }         // If target is smaller, ignore the right half         else {             right = mid - 1;         }     }     // Target is not present in the array     return -1; } // Function to print the array void printArra...

Write a c program to perform linear Search

 #include <stdio.h> // Function to perform linear search int linearSearch(int arr[], int n, int target) {     for (int i = 0; i < n; i++) {         if (arr[i] == target) {             return i; // Return the index of the target element         }     }     return -1; // Return -1 if the target element is not found } // Function to print the array void printArray(int arr[], int size) {     for (int i = 0; i < size; i++) {         printf("%d ", arr[i]);     }     printf("\n"); } int main() {     int n, target;     // Input the number of elements in the array     printf("Enter the number of elements: ");     scanf("%d", &n);     int arr[n];     // Input the elements of the array     printf("Enter the elements: ");     for (int i = 0; i < n; ...

Write a c program to sort elements in row wise and print the elements of matrix in Column major order

 #include <stdio.h> #include <stdlib.h> // Function to sort a row in ascending order void sortRowAscending(int *row, int n) {     for (int i = 0; i < n - 1; i++) {         for (int j = i + 1; j < n; j++) {             if (row[i] > row[j]) {                 int temp = row[i];                 row[i] = row[j];                 row[j] = temp;             }         }     } } int main() {     int m, n;     printf("Enter the number of rows and columns: ");     scanf("%d %d", &m, &n);     int matrix[m][n];     printf("Enter the elements of the matrix:\n");     for (int i = 0; i < m; i++) {         for (int j = 0; j < n; j++) {     ...

Write a c program to sort in ascending order and reverse the individual row elements of an mxn matrix

 #include <stdio.h> #include <stdlib.h> // Function to sort a row in ascending order void sortRowAscending(int *row, int n) {     for (int i = 0; i < n - 1; i++) {         for (int j = i + 1; j < n; j++) {             if (row[i] > row[j]) {                 int temp = row[i];                 row[i] = row[j];                 row[j] = temp;             }         }     } } // Function to reverse a row void reverseRow(int *row, int n) {     int start = 0, end = n - 1;     while (start < end) {         int temp = row[start];         row[start] = row[end];         row[end] = temp;         start++;         end--;...

Write a C Program to implement Merge Sort

 #include <stdio.h> #include <stdlib.h> // Function to merge two subarrays void merge(int arr[], int left, int mid, int right) {     int n1 = mid - left + 1;     int n2 = right - mid;     // Create temporary arrays     int L[n1], R[n2];     // Copy data to temporary arrays L[] and R[]     for (int i = 0; i < n1; i++)         L[i] = arr[left + i];     for (int j = 0; j < n2; j++)         R[j] = arr[mid + 1 + j];     // Merge the temporary arrays back into arr[left..right]     int i = 0; // Initial index of first subarray     int j = 0; // Initial index of second subarray     int k = left; // Initial index of merged subarray     while (i < n1 && j < n2) {         if (L[i] <= R[j]) {             arr[k] = L[i];          ...

Write a c program to sort the given n integers and perform following operations Find the products of every two odd position elements, Find the sum of every two even position elements Explanation

 #include <stdio.h> #include <stdlib.h> // Function to sort the array void sortArray(int arr[], int n) {     for (int i = 0; i < n - 1; i++) {         for (int j = i + 1; j < n; j++) {             if (arr[i] > arr[j]) {                 int temp = arr[i];                 arr[i] = arr[j];                 arr[j] = temp;             }         }     } } // Function to print the array void printArray(int arr[], int size) {     for (int i = 0; i < size; i++)         printf("%d ", arr[i]);     printf("\n"); } // Function to find the product of every two odd position elements void productOfOddPositionElements(int arr[], int n) {     printf("Products of every two odd position element...

Write a c program to implement Insertion quick sort

 #include <stdio.h> void swap(int* a, int* b) {     int t = *a;     *a = *b;     *b = t; } int partition(int arr[], int low, int high) {     int pivot = arr[high]; // pivot     int i = (low - 1); // Index of smaller element     for (int j = low; j <= high - 1; j++) {         // If current element is smaller than or equal to pivot         if (arr[j] <= pivot) {             i++; // increment index of smaller element             swap(&arr[i], &arr[j]);         }     }     swap(&arr[i + 1], &arr[high]);     return (i + 1); } void quickSort(int arr[], int low, int high) {     if (low < high) {         // pi is partitioning index, arr[p] is now at right place         int pi = partition(arr, low, high);...

Write a c program to implement Insertion sort

 #include <stdio.h> void insertionSort(int arr[], int n) {     for (int i = 1; i < n; i++) {         int key = arr[i];         int j = i - 1;         // Move elements of arr[0..i-1], that are greater than key,         // to one position ahead of their current position         while (j >= 0 && arr[j] > key) {             arr[j + 1] = arr[j];             j = j - 1;         }         arr[j + 1] = key;     } } // Function to print the array void printArray(int arr[], int size) {     for (int i = 0; i < size; i++)         printf("%d ", arr[i]);     printf("\n"); } int main() {     int arr[] = {12, 11, 13, 5, 6};     int n = sizeof(arr) / sizeof(arr[0]);         ...

Write a C program to reverse the elements within a given range in a sorted list. Example : input : 10 9 1 2 4 3 4 6 7 8 10 3 8 output: 1 2 8 7 6 4 4 3 9 10 the sorted list of given array elements is 1 2 3 4 4 6 7 8 9 10 , after reversing the elements with in the range 3 and 8 is 1 2 8 7 6 4 4 3 9 10

 #include <stdio.h> #include <stdlib.h> // Function to sort the array void sortArray(int arr[], int n) {     for (int i = 0; i < n-1; i++) {         for (int j = i+1; j < n; j++) {             if (arr[i] > arr[j]) {                 int temp = arr[i];                 arr[i] = arr[j];                 arr[j] = temp;             }         }     } } // Function to reverse elements within a given range void reverseRange(int arr[], int start, int end) {     while (start < end) {         int temp = arr[start];         arr[start] = arr[end];         arr[end] = temp;         start++;         end--;     } } // Funct...

Write a c program to implement selection bubble sort

 #include <stdio.h> void bubbleSort(int arr[], int n) {     int i, j;     for (i = 0; i < n-1; i++) {         // Last i elements are already in place         for (j = 0; j < n-i-1; j++) {             if (arr[j] > arr[j+1]) {                 // Swap arr[j] and arr[j+1]                 int temp = arr[j];                 arr[j] = arr[j+1];                 arr[j+1] = temp;             }         }     } } // Function to print the array void printArray(int arr[], int size) {     for (int i = 0; i < size; i++)         printf("%d ", arr[i]);     printf("\n"); } int main() {     int arr[] = {64, 34, 25, 12, 22, 11, 90};...

Write a c program to implement selection Sort

 #include <stdio.h> void selectionSort(int arr[], int n) {     int i, j, min_idx;     // One by one move the boundary of the unsorted subarray     for (i = 0; i < n-1; i++) {         // Find the minimum element in the unsorted array         min_idx = i;         for (j = i+1; j < n; j++)             if (arr[j] < arr[min_idx])                 min_idx = j;         // Swap the found minimum element with the first element         int temp = arr[min_idx];         arr[min_idx] = arr[i];         arr[i] = temp;     } } // Function to print the array void printArray(int arr[], int size) {     for (int i = 0; i < size; i++)         printf("%d ", arr[i]);     printf("\n"); } int main() { ...

Write a C program to store n employee records based on EMP_ID,EMP_NAME,EMP_DEPTID,EMP_PHNO,EMP_SALARY and display all the details of employees using EMP_NAME in sorted order

 #include <stdio.h> #include <stdlib.h> #include <string.h> // Define a structure to represent an employee typedef struct {     int emp_id;     char emp_name[50];     int emp_deptid;     char emp_phno[15];     float emp_salary; } Employee; // Function to read employee details void readEmployee(Employee *employee) {     printf("Enter Employee ID: ");     scanf("%d", &employee->emp_id);     printf("Enter Employee Name: ");     scanf(" %[^\n]%*c", employee->emp_name);  // Read string with spaces     printf("Enter Employee Department ID: ");     scanf("%d", &employee->emp_deptid);     printf("Enter Employee Phone Number: ");     scanf(" %[^\n]%*c", employee->emp_phno);     printf("Enter Employee Salary: ");     scanf("%f", &employee->emp_salary); } // Function to display employee details v...

Write a c program to store records of n students based on roll_no, name, gender and 5 subject marks Calculate percentage each student using 5 subjects., Display the student list according to their percentages

 #include <stdio.h> #include <stdlib.h> #include <string.h> // Define a structure to represent a student typedef struct {     int roll_no;     char name[50];     char gender;     int marks[5];     float percentage; } Student; // Function to read student details void readStudent(Student *student) {     printf("Enter roll number: ");     scanf("%d", &student->roll_no);     printf("Enter name: ");     scanf(" %[^\n]%*c", student->name);  // Read string with spaces     printf("Enter gender (M/F): ");     scanf(" %c", &student->gender);     printf("Enter marks for 5 subjects: ");     for (int i = 0; i < 5; i++) {         scanf("%d", &student->marks[i]);     } } // Function to calculate percentage void calculatePercentage(Student *student) {     int total = 0;   ...

Write a C program that uses functions to perform the following Operations Reading a complex number, Writing a complex number, Addition of two complex numbers, Multiplication of two complex numbers

 #include <stdio.h> // Define a structure to represent a complex number typedef struct {     float real;     float imag; } Complex; // Function to read a complex number Complex readComplex() {     Complex c;     printf("Enter real part: ");     scanf("%f", &c.real);     printf("Enter imaginary part: ");     scanf("%f", &c.imag);     return c; } // Function to write (print) a complex number void writeComplex(Complex c) {     if (c.imag >= 0)         printf("%.2f + %.2fi\n", c.real, c.imag);     else         printf("%.2f - %.2fi\n", c.real, -c.imag); } // Function to add two complex numbers Complex addComplex(Complex c1, Complex c2) {     Complex result;     result.real = c1.real + c2.real;     result.imag = c1.imag + c2.imag;     return result; } // Function to multiply two complex numbers...

Write a c program to demonstrate pre-processor directives Macros

 #include <stdio.h> // Define a macro for a constant #define PI 3.14159 // Define a macro for a function-like macro #define SQUARE(x) ((x) * (x)) // Conditional compilation #define DEBUG 1 int main() {     // Using the constant macro     printf("Value of PI: %f\n", PI);     // Using the function-like macro     int num = 5;     printf("Square of %d: %d\n", num, SQUARE(num));     // Conditional compilation     #if DEBUG         printf("Debug mode is ON\n");     #else         printf("Debug mode is OFF\n");     #endif     return 0; }

Write a to display present date and time using c language.

 #include <stdio.h> #include <time.h> int main() {     // Get the current time     time_t now = time(NULL);          // Check if time() returns an error     if (now == -1) {         printf("Failed to get the current time.\n");         return 1;     }     // Convert time to local time structure     struct tm *local = localtime(&now);          // Check if localtime() returns NULL     if (local == NULL) {         printf("Failed to convert to local time.\n");         return 1;     }     // Print the current date and time in the format: YYYY-MM-DD HH:MM:SS     printf("Current date and time: %04d-%02d-%02d %02d:%02d:%02d\n",            local->tm_year + 1900,  // Year since 1900         ...

Write a c program on 2D array to Increase & Decrease elements in the subarrays

 #include <stdio.h> #include <stdlib.h> // Function to increase the number of elements in a specific row of a 2D array void increaseElementsInRow(int** array, int row, int currentCols, int newCols) {     array[row] = (int*)realloc(array[row], newCols * sizeof(int));     for (int j = currentCols; j < newCols; j++) {         array[row][j] = 0; // Initialize new elements with 0     } } // Function to decrease the number of elements in a specific row of a 2D array void decreaseElementsInRow(int** array, int row, int newCols) {     array[row] = (int*)realloc(array[row], newCols * sizeof(int)); } // Function to print the 2D array void printArray(int** array, int rows, int* cols) {     for (int i = 0; i < rows; i++) {         for (int j = 0; j < cols[i]; j++) {             printf("%d ", array[i][j]);         }      ...