Wednesday, 22 January 2025

Basic-3 C programming

Course Practical List-Programming for Problem Solving 

1.Write a program that performs basic arithmetic operations (addition, subtraction, multiplication, and division) and demonstrates the use of different data types.

 

#include <stdio.h>

 int main() {

    int a = 10, b = 3;

    float div;

     printf("Addition: %d\n", a + b);

    printf("Subtraction: %d\n", a - b);

    printf("Multiplication: %d\n", a * b);

     div = (float)a / b;

    printf("Division: %.2f\n", div);

     return 0;

}

  

2.Create a program that uses if, else, and switch statements to implement a simple menu-driven application. Use loops (for, while, and do-while) to repeat tasks.

 

#include <stdio.h>

 int main() {

    int choice, a, b;

     do {

        printf("\n1.Add\n2.Subtract\n3.Multiply\n4.Exit\n");

        printf("Enter choice: ");

        scanf("%d", &choice);

         if (choice >= 1 && choice <= 3) {

            printf("Enter two numbers: ");

            scanf("%d %d", &a, &b);

        }

         switch (choice) {

            case 1: printf("Sum = %d\n", a + b); break;

            case 2: printf("Difference = %d\n", a - b); break;

            case 3: printf("Product = %d\n", a * b); break;

            case 4: printf("Exiting...\n"); break;

            default: printf("Invalid choice\n");

        }

    } while (choice != 4);

     return 0;

}

  

Develop a program that calculates the factorial of a number using both iterative and recursive functions.

 

#include <stdio.h>

 int factRec(int n) {

    if (n == 0) return 1;

    return n * factRec(n - 1);

}

 int main() {

    int n, fact = 1;

     printf("Enter number: ");

    scanf("%d", &n);

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

        fact *= i;

     printf("Iterative Factorial: %d\n", fact);

    printf("Recursive Factorial: %d\n", factRec(n));

     return 0;

}

  Write a program to perform various operations on arrays (e.g., sorting, searching) and strings (e.g., concatenation, comparison).

 

#include <stdio.h>

#include <string.h>

 int main() {

    int a[5] = {5, 3, 1, 4, 2}, temp;

    char s1[20] = "Hello";

    char s2[20] = "World";

     // Sorting array

    for (int i = 0; i < 5; i++)

        for (int j = i + 1; j < 5; j++)

            if (a[i] > a[j]) {

                temp = a[i];

                a[i] = a[j];

                a[j] = temp;

            }

    printf("Sorted Array: ");

    for (int i = 0; i < 5; i++)

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

     // String operations

    strcat(s1, s2);

    printf("\nConcatenation: %s", s1);

    printf("\nComparison: %d\n", strcmp("abc", "abc"));

     return 0;

}

Implement a program that uses pointers to create and manipulate dynamic arrays, demonstrating the useof malloc, calloc, realloc, and free.

 

#include <stdio.h>

#include <stdlib.h>

 int main() {

    int *a, n = 3;

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

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

        a[i] = i + 1;

     a = (int *)realloc(a, 5 * sizeof(int));

    a[3] = 4;

    a[4] = 5;

     for (int i = 0; i < 5; i++)

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

     free(a);

    return 0;

}

 

Design a student record system using structures that store and display information such as name, roll number, and grades.

 

#include <stdio.h>

 struct student {

    char name[20];

    int roll;

    float marks;

};

 int main() {

    struct student s;

     printf("Enter name, roll, marks: ");

    scanf("%s %d %f", s.name, &s.roll, &s.marks);

     printf("Name: %s\nRoll: %d\nMarks: %.2f\n",

           s.name, s.roll, s.marks);

     return 0;

}

 

Write a program to read from and write to files, such as creating a simple text editor that performs basic file operations.

 

#include <stdio.h>

 int main() {

    FILE *fp;

    char text[50];

     fp = fopen("file.txt", "w");

    fprintf(fp, "Hello File");

    fclose(fp);

     fp = fopen("file.txt", "r");

    fscanf(fp, "%s", text);

    printf("File Content: %s", text);

    fclose(fp);

     return 0;

}

 

Implement a singly linked list with operations like insertion, deletion, and traversal.

#include <stdio.h>

#include <stdlib.h>

 struct node {

    int data;

    struct node *next;

};

 int main() {

    struct node *head = NULL, *newnode, *temp;

    int n = 3;

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

        newnode = (struct node *)malloc(sizeof(struct node));

        newnode->data = i + 1;

        newnode->next = NULL;

         if (head == NULL)

            head = newnode;

        else {

            temp = head;

            while (temp->next)

                temp = temp->next;

            temp->next = newnode;

        }

    }

 

    temp = head;

    while (temp) {

        printf("%d ", temp->data);

        temp = temp->next;

    }

    return 0;

 

Develop programs to simulate stack operations (push, pop, peek) and queue operations (enqueue, dequeue) using arrays and linked lists.

 

#include <stdio.h>

 int main() {

    int stack[5], top = -1;

     stack[++top] = 10;

    stack[++top] = 20;

    printf("Popped: %d\n", stack[top--]);

 

    int queue[5], front = 0, rear = 0;

    queue[rear++] = 5;

    queue[rear++] = 15;

    printf("Dequeued: %d\n", queue[front++]);

     return 0;

}

 

Provide students with a program containing intentional errors and inefficiencies. Have them use debugging tools (like gdb) to find and fix the errors and optimize the code for better performance.

 #include <stdio.h>

 int main() {

    int i;

    for (i = 0; i <= 5; i++)   // error: should be i < 5

        printf("%d ", i);

    return 0;

}