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;

}



Tuesday, 21 January 2025

Basic-2 C programming

 16. Program to Check Prime Number

#include <stdio.h>

#include<conio.h>

int main() {

    int num = 7, i, flag = 0;

    for (i = 2; i <= num / 2; i++) {

        if (num % i == 0) {

            flag = 1;

            break;

        }

    }

    if (flag == 0)

        printf("Prime number");

    else

        printf("Not a prime number");


    return 0;

    getch();

}


Output

Prime number


17. Program to Print Prime Numbers from 1 to 10

 #include <stdio.h>

#include<conio.h>

int main() {

    int i, j, flag;

    for (i = 2; i <= 10; i++) {

        flag = 0;

        for (j = 2; j <= i / 2; j++) {

            if (i % j == 0) {

                flag = 1;

                break;

            }

        }

        if (flag == 0)

            printf("%d ", i);

    }

    return 0;

     getch();

}


Output

2 3 5 7


18. Program to Find Fibonacci Series

 #include <stdio.h>

#include<conio.h>

int main() {

    int n = 5, a = 0, b = 1, c, i;

    printf("%d %d ", a, b);

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

        c = a + b;

        printf("%d ", c);

        a = b;

        b = c;

    }

    return 0;

    getch();

}


Output

0 1 1 2 3


19. Program Using Function (Addition)

#include <stdio.h>

#include<conio.h>

int add(int a, int b) {

    return a + b;

}

int main() {

    int result;

    result = add(10, 20);

    printf("Sum = %d", result);

    return 0;

    getch();

}


Output

Sum = 30


20. Program to Find String Length (Without strlen)

 #include <stdio.h>

#include<conio.h>

int main() {

    char str[] = "Hello";

    int i = 0;

    while (str[i] != '\0') {

        i++;

    }

    printf("Length = %d", i);

    return 0;

    getch();

}


Output

Length = 5


21. Program to Reverse a String

 #include <stdio.h>

#include<conio.h>

int main() {

    char str[] = "Cprogram";

    int i, len = 0;


    while (str[len] != '\0') {

        len++;

    }

    for (i = len - 1; i >= 0; i--) {

        printf("%c", str[i]);

    }

    return 0;

    getch();

}


Output

margorpC


22. Program to Copy One String to Another

 #include <stdio.h>

#include<conio.h>

int main() {

    char str1[] = "Hello";

    char str2[10];

    int i = 0;

    while (str1[i] != '\0') {

        str2[i] = str1[i];

        i++;

    }

    str2[i] = '\0';

    printf("Copied string: %s", str2);

    return 0;

    getch();

}


Output

Copied string: Hello


23. Program Using Pointer

 #include <stdio.h>

#include<conio.h>

int main() {

    int a = 10;

    int *p;

    p = &a;

    printf("Value of a = %d\n", a);

    printf("Value using pointer = %d", *p);

    return 0;

    getch();

}


Output

Value of a = 10

Value using pointer = 10


24. Program to Swap Two Numbers (Using Function)

 #include <stdio.h>

#include<conio.h>

void swap(int *x, int *y) {

    int temp;

    temp = *x;

    *x = *y;

    *y = temp;

}

int main() {

    int a = 5, b = 10;

    swap(&a, &b);

    printf("a = %d, b = %d", a, b);

    return 0;

    getch();

}


Output

a = 10, b = 5




Monday, 20 January 2025

Basic-1 C programming

 1. Hello World Program

#include <stdio.h>

#include<conio.h>

int main() {

    printf("Hello, World!");

    return 0;

    getch();

}

Output

Hello, World!


2. Program to Add Two Numbers

#include <stdio.h>

#include<conio.h>

int main() {

    int a, b, sum;

    a = 10;

    b = 20;

    sum = a + b;

    printf("Sum = %d", sum);

    return 0;

    getch();

}

Output

Sum = 30


3. Program to Find Even or Odd Number

#include <stdio.h>

#include<conio.h>

int main() {

    int num = 7;

    if (num % 2 == 0)

        printf("Even number");

    else

        printf("Odd number");


    return 0;

     getch();

}

Output

Odd number


4. Program to Find Largest of Two Numbers

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 15, b = 25;


    if (a > b)

        printf("A is greater");

    else

        printf("B is greater");

    return 0;

    getch();

}

Output

B is greater


5. Program to Print Numbers from 1 to 5 (Using Loop)

#include <stdio.h>

#include<conio.h>

int main() {

    int i;

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

        printf("%d ", i);

    }

    return 0;

}

Output

1 2 3 4 5


6. Program to Find Factorial of a Number

#include <stdio.h>

#include<conio.h>

int main() {

    int i, n = 5;

    int fact = 1;


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

        fact = fact * i;

    }

    printf("Factorial = %d", fact);

    return 0;

    getch();

}


Output

Factorial = 120


7. Program to Check Vowel or Consonant

#include <stdio.h>

#include<conio.h>

int main() {

    char ch = 'a';

    if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

        printf("Vowel");

    else

        printf("Consonant");

    return 0;

    gecth();

}


Output

Vowel

8. Program to Take Input from User

#include <stdio.h>

#include<conio.h>


int main() {

    int num;

    printf("Enter a number: ");

    scanf("%d", &num);


    printf("You entered: %d", num);

    return 0;

    gecth();

}

Output

Enter a number: 5

You entered: 5


9. Program to Find Largest of Three Numbers

 #include <stdio.h>

#include<conio.h>

int main() {

    int a, b, c;

    a = 10;

    b = 25;

    c = 15;

    if (a > b && a > c)

        printf("A is largest");

    else if (b > c)

        printf("B is largest");

    else

        printf("C is largest");


    return 0;

    gecth();

}


Output

B is largest


10. Program to Print Multiplication Table

 #include <stdio.h>

#include<conio.h>

int main() {

    int i, n = 5;


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

        printf("%d x %d = %d\n", n, i, n * i);

    }


    return 0;

    getch();

}

Output

5 x 1 = 5

5 x 2 = 10

...

5 x 10 = 50

11. Program to Reverse a Number

 #include <stdio.h>

#include<conio.h>

int main() {

    int num = 123, rev = 0;


    while (num != 0) {

        rev = rev * 10 + num % 10;

        num = num / 10;

    }

    printf("Reversed number = %d", rev);

    return 0;

    gecth();

}


Output

Reversed number = 321


12. Program to Check Palindrome Number

 #include <stdio.h>

#include<conio.h>

int main() {

    int num = 121, temp, rev = 0;

    temp = num;

    while (num != 0) {

        rev = rev * 10 + num % 10;

        num = num / 10;

    }

    if (temp == rev)

        printf("Palindrome number");

    else

        printf("Not a palindrome");


    return 0;

    getch();

}


Output

Palindrome number


13. Program to Find Sum of Digits

 #include <stdio.h>

#include<conio.h>

int main() {

    int num = 123, sum = 0;


    while (num != 0) {

        sum = sum + num % 10;

        num = num / 10;

    }

    printf("Sum of digits = %d", sum);

    return 0;

    getch();

}


Output

Sum of digits = 6


14. Program Using Array (Print Elements)

 #include <stdio.h>

#include<conio.h>

int main() {

    int i;

    int arr[5] = {10, 20, 30, 40, 50};

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

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

    }

    return 0;

    getch();

}


Output

10 20 30 40 50


15. Program to Find Sum of Array Elements

 #include <stdio.h>

#include<conio.h>

int main() {

    int i, sum = 0;

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

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

        sum = sum + arr[i];

    }

    printf("Sum = %d", sum);

    return 0;

getch():

}


Output

Sum = 15