0% found this document useful (0 votes)
5 views24 pages

Himanshu

The document is a practical file for a Data Structures Lab course, containing various C programming exercises. It includes programs for checking Armstrong numbers, generating Fibonacci series, defining structures and unions, dynamic memory allocation, linked lists, and basic array operations. Each program is accompanied by code snippets and explanations of their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views24 pages

Himanshu

The document is a practical file for a Data Structures Lab course, containing various C programming exercises. It includes programs for checking Armstrong numbers, generating Fibonacci series, defining structures and unions, dynamic memory allocation, linked lists, and basic array operations. Each program is accompanied by code snippets and explanations of their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Practical File

Data Structures Lab (PBC 201)


BCA 2nd Semester
Session 2024-2025

Submitted To:
Submitted By:
Mr.Saurabh Dhanik
Himanshu Nath
Assistant Professor BCA
School of Computing Section:
c
Graphic Era Hill University Roll
No.________ Bhimtal Campus
1 .Write a C program to check if a given number is an Armstrong number.

#include <stdio.h>

int main() {

int num, originalNum, remainder, result = 0;

printf("Enter a three-digit integer: ");

scanf("%d", &num);

originalNum = num;

while (originalNum != 0) {

// remainder contains the last digit

remainder = originalNum % 10;

result += remainder * remainder * remainder;

// removing last digit from the orignal number

originalNum /= 10;

if (result == num)

printf("%d is an Armstrong number.", num);

else

printf("%d is not an Armstrong number.", num);

return 0;

}
1. Write a C program to generate the Fibonacci series up to a given number of terms.

Answer :

#include <stdio.h>

int main() {

int i, n;

// initialize first and second terms

int t1 = 0, t2 = 1;

// initialize the next term (3rd term)

int nextTerm = t1 + t2;

// get no. of terms from user

printf("Enter the number of terms: ");

scanf("%d", &n);

// print the first two terms t1 and t2

printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms

for (i = 3; i <= n; ++i) {

printf("%d, ", nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

return 0;

}
3. Define a structure for a student with attributes (name, roll number, marks) and display the student
details.

#include <stdio.h>

// Define a structure for a student

struct Student {

char name[50]; // Student's name

int rollNumber; // Roll number

float marks; // Marks obtained

};

int main() {

struct Student student; // Declare a variable of type Student

// Input student details

printf("Enter student's name: ");

fgets(student.name, sizeof(student.name), stdin); // Using fgets to input name (to allow spaces)

printf("Enter roll number: ");

scanf("%d", &student.rollNumber);

printf("Enter marks: ");

scanf("%f", &student.marks);

// Display student details

printf("\n--- Student Details ---\n");

printf("Name: %s", student.name); // %s to print string, fgets includes the newline at the end

printf("Roll Number: %d\n", student.rollNumber);

printf("Marks: %.2f\n", student.marks);

return 0;

}
4 Define a union for employee details (name, ID, salary) and show how memory is shared.

#include <stdio.h>

#include <string.h>

// Define a union for employee details

union Employee {

char name[50]; // Employee's name

int ID; // Employee's ID

float salary; // Employee's salary

};

int main() {

// Declare a variable of type union Employee

union Employee emp;

// Input employee details using the union members

printf("Enter employee name: ");

fgets(emp.name, sizeof(emp.name), stdin); // Input name using fgets (to allow spaces)

printf("Enter employee ID: ");

scanf("%d", &emp.ID);

printf("Enter employee salary: ");

scanf("%f", &emp.salary);

// Display the employee details

printf("\n--- Employee Details ---\n");

printf("Name: %s", emp.name); // It will print the name entered

printf("ID: %d\n", emp.ID); // It will print the ID entered

printf("Salary: %.2f\n", emp.salary); // It will print the salary entered


// Demonstrating how memory is shared:

printf("\nMemory Sharing Demonstration:\n");

printf("Size of union Employee: %zu bytes\n", sizeof(emp));

// Printing values of all members after assigning each

printf("Name: %s", emp.name); // Will show the name, but ID and Salary may be overwritten

printf("ID: %d\n", emp.ID); // Will show the ID, but Salary may be overwritten

printf("Salary: %.2f\n", emp.salary); // Will show the salary, but Name and ID may be overwritten

return 0;

}
5. Write a C program to dynamically allocate memory for an array using malloc, accept user input,
and print the values.

#include <stdio.h>

#include <stdlib.h> // For malloc() and free()

int main() {

int *arr; // Pointer to hold the dynamically allocated array

int n, i;

// Accept the number of elements for the array

printf("Enter the number of elements: ");

scanf("%d", &n);

// Dynamically allocate memory for the array

arr = (int *)malloc(n * sizeof(int));

// Check if memory allocation was successful

if (arr == NULL) {

printf("Memory allocation failed!\n");

return 1; // Exit the program if memory allocation fails

// Accept user input for the array elements

printf("Enter %d elements:\n", n);

for (i = 0; i < n; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);

// Print the values stored in the array


printf("\nArray values:\n");

for (i = 0; i < n; i++) {

printf("Element %d: %d\n", i + 1, arr[i]);

// Free the dynamically allocated memory

free(arr);

return 0;

}
6. Write a C program to dynamically allocate memory for an array using calloc and demonstrate the
difference from malloc.

#include <stdio.h>

#include <stdlib.h> // For malloc(), calloc(), free()

int main() {

int *arr_malloc, *arr_calloc;

int n, i;

// Accept the number of elements for the arrays

printf("Enter the number of elements: ");

scanf("%d", &n);

// Dynamically allocate memory for arrays using malloc and calloc

arr_malloc = (int *)malloc(n * sizeof(int)); // Using malloc

arr_calloc = (int *)calloc(n, sizeof(int)); // Using calloc

// Check if memory allocation for malloc was successful

if (arr_malloc == NULL) {

printf("Memory allocation using malloc failed!\n");

return 1;

// Check if memory allocation for calloc was successful

if (arr_calloc == NULL) {

printf("Memory allocation using calloc failed!\n");

return 1;

// Accept user input for the array elements


printf("Enter elements for the malloc array:\n");

for (i = 0; i < n; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr_malloc[i]);

// Print the values in malloc array

printf("\nValues in malloc array:\n");

for (i = 0; i < n; i++) {

printf("Element %d: %d\n", i + 1, arr_malloc[i]);

// Print the values in calloc array (should be initialized to 0)

printf("\nValues in calloc array:\n");

for (i = 0; i < n; i++) {

printf("Element %d: %d\n", i + 1, arr_calloc[i]); // Should print 0

// Free the dynamically allocated memory

free(arr_malloc);

free(arr_calloc);

return 0;

}
7. Write a C program to dynamically resize an allocated memory block using realloc.

#include <stdio.h>

#include <stdlib.h> // For malloc(), realloc(), free()

int main() {

int *arr;

int initial_size, new_size, i;

// Input the initial size of the array

printf("Enter the initial number of elements: ");

scanf("%d", &initial_size);

// Dynamically allocate memory for the array using malloc

arr = (int *)malloc(initial_size * sizeof(int));

if (arr == NULL) {

printf("Memory allocation failed!\n");

return 1; // Exit the program if malloc fails

// Input values for the array

printf("Enter %d elements:\n", initial_size);

for (i = 0; i < initial_size; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);

// Display the current array elements

printf("\nArray values before resizing:\n");

for (i = 0; i < initial_size; i++) {

printf("Element %d: %d\n", i + 1, arr[i]);


}

// Input the new size for the array

printf("\nEnter the new number of elements (after resizing): ");

scanf("%d", &new_size);

// Dynamically resize the memory block using realloc

arr = (int *)realloc(arr, new_size * sizeof(int));

if (arr == NULL) {

printf("Memory reallocation failed!\n");

return 1; // Exit the program if realloc fails

// Input new values if the size was increased

if (new_size > initial_size) {

printf("Enter additional %d elements:\n", new_size - initial_size);

for (i = initial_size; i < new_size; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);

// Display the array values after resizing

printf("\nArray values after resizing:\n");

for (i = 0; i < new_size; i++) {

printf("Element %d: %d\n", i + 1, arr[i]);

// Free the dynamically allocated memory

free(arr);

return 0;

}
8. Write a C program to create a singly linked list and display the elements.

#include <stdio.h>

#include <stdlib.h>

// Define the structure of a node

struct Node {

int data; // Data part of the node

struct Node* next; // Pointer to the next node

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Allocate memory for new
node

if (newNode == NULL) { // Check if memory allocation is successful

printf("Memory allocation failed!\n");

exit(1); // Exit if memory allocation fails

newNode->data = data; // Set the data

newNode->next = NULL; // Initially set next to NULL

return newNode; // Return the new node

// Function to display the linked list

void displayList(struct Node* head) {

if (head == NULL) {

printf("The list is empty.\n");

return;

struct Node* temp = head; // Temporary pointer to traverse the list


while (temp != NULL) {

printf("%d -> ", temp->data); // Print the data of the current node

temp = temp->next; // Move to the next node

printf("NULL\n"); // End of the list

int main() {

struct Node* head = NULL; // Initialize head of the list as NULL

struct Node* temp = NULL; // Temporary pointer for inserting nodes

int n, data;

// Accept the number of elements in the list

printf("Enter the number of elements in the list: ");

scanf("%d", &n);

// Create the linked list by adding nodes

for (int i = 0; i < n; i++) {

printf("Enter data for node %d: ", i + 1);

scanf("%d", &data);

// Create a new node

struct Node* newNode = createNode(data);

// If the list is empty, make newNode the head

if (head == NULL) {

head = newNode;

} else {

// Traverse to the last node and append newNode

temp = head;

while (temp->next != NULL) {


temp = temp->next; // Move to the next node

temp->next = newNode; // Link the new node

// Display the linked list

printf("\nThe linked list is:\n");

displayList(head);

return 0;

}
9. Write a C program to find the largest element in an array.

#include <stdio.h>

int main() {

int n, i, largest;

// Input the number of elements in the array

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n]; // Declare an array of size n

// Input array elements

printf("Enter %d elements:\n", n);

for (i = 0; i < n; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);

// Assume the first element is the largest

largest = arr[0];

// Traverse the array to find the largest element

for (i = 1; i < n; i++) {

if (arr[i] > largest) {

largest = arr[i]; // Update largest if current element is greater

// Output the largest element


printf("\nThe largest element in the array is: %d\n", largest);

return 0;

}
10. Write a C program to check if a number is even or odd using bitwise operators.

#include <stdio.h>

int main() {

int num;

// Input a number from the user

printf("Enter a number: ");

scanf("%d", &num);

// Check if the number is even or odd using bitwise operator

if (num & 1) {

printf("%d is odd.\n", num);

} else {

printf("%d is even.\n", num);

return 0;
11. Write a C program to read n elements in an array and perform the following operations: - Insert
an element at a given position. - Delete an element from a given position. - Display the array
elements.

#include <stdio.h>

// Function to display the array elements

void displayArray(int arr[], int n) {

if (n == 0) {

printf("Array is empty.\n");

return;

printf("Array elements: ");

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

// Function to insert an element at a given position

void insertElement(int arr[], int *n, int element, int position) {

if (position > *n || position < 0) {

printf("Invalid position.\n");

return;

// Shift elements to the right to make space for the new element

for (int i = *n; i > position; i--) {

arr[i] = arr[i - 1];

// Insert the new element at the specified position


arr[position] = element;

// Increment the size of the array

(*n)++;

// Function to delete an element from a given position

void deleteElement(int arr[], int *n, int position) {

if (position >= *n || position < 0) {

printf("Invalid position.\n");

return;

// Shift elements to the left to fill the gap

for (int i = position; i < *n - 1; i++) {

arr[i] = arr[i + 1];

// Decrement the size of the array

(*n)--;

// Main function

int main() {

int arr[100], n, choice, element, position;

// Input the number of elements in the array

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

// Input the elements of the array


printf("Enter %d elements:\n", n);

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

do {

// Display menu options

printf("\nMenu:\n");

printf("1. Insert element at a given position\n");

printf("2. Delete element from a given position\n");

printf("3. Display array elements\n");

printf("4. Exit\n");

// Accept user's choice

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

// Insert an element at a given position

printf("Enter the element to insert: ");

scanf("%d", &element);

printf("Enter the position (0 to %d): ", n);

scanf("%d", &position);

insertElement(arr, &n, element, position);

break;

case 2:

// Delete an element from a given position

printf("Enter the position (0 to %d): ", n - 1);

scanf("%d", &position);
deleteElement(arr, &n, position);

break;

case 3:

// Display the array elements

displayArray(arr, n);

break;

case 4:

// Exit the program

printf("Exiting the program...\n");

break;

default:

printf("Invalid choice. Please try again.\n");

} while (choice != 4);

return 0;

}
12. Write a C program to read an m × n matrix and find its transpose

#include <stdio.h>

int main() {

int m, n;

// Input the dimensions of the matrix (m x n)

printf("Enter the number of rows (m): ");

scanf("%d", &m);

printf("Enter the number of columns (n): ");

scanf("%d", &n);

int matrix[m][n], transpose[n][m]; // Define the matrix and its transpose

// Input the elements of the matrix

printf("Enter the elements of the matrix (%d x %d):\n", m, n);

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

printf("Element [%d][%d]: ", i + 1, j + 1);

scanf("%d", &matrix[i][j]);

// Compute the transpose of the matrix

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

transpose[j][i] = matrix[i][j];

}
// Display the original matrix

printf("\nOriginal Matrix (%d x %d):\n", m, n);

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

printf("%d ", matrix[i][j]);

printf("\n");

// Display the transpose of the matrix

printf("\nTranspose of the Matrix (%d x %d):\n", n, m);

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++) {

printf("%d ", transpose[i][j]);

printf("\n");

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy