C Programming Lab Manual

Course Code: 1BPOPL107/207

Artificial Intelligence and Data Science Department, AITM, BGM

PART – A: CONVENTIONAL EXPERIMENTS

Program 1: Distance Between Two Points

Calculate the straight-line distance between two points on a 2D plane.

Algorithm

  1. Start the program.
  2. Declare x1, y1, x2, y2 and distance.
  3. Read the coordinates of the first point.
  4. Read the coordinates of the second point.
  5. Calculate the distance using the distance formula.
  6. Display the calculated distance.
  7. Stop the program.

Source Code

#include <stdio.h>
#include <math.h>

int main()
{
    float x1, y1, x2, y2, distance;

    printf("Enter coordinates of first point (x1 y1): ");
    scanf("%f %f", &x1, &y1);

    printf("Enter coordinates of second point (x2 y2): ");
    scanf("%f %f", &x2, &y2);

    distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

    printf("Distance between the two points = %.2f", distance);

    return 0;
}

Sample Output

Enter coordinates of first point (x1 y1): 2 3
Enter coordinates of second point (x2 y2): 6 7
Distance between the two points = 5.66

Program 2: Student Grade Calculation

Display the grade based on the marks obtained by a student.

Algorithm

  1. Read the student's marks.
  2. If marks are 90 or above, display Grade A.
  3. Else if marks are 75 or above, display Grade B.
  4. Else if marks are 60 or above, display Grade C.
  5. Else if marks are 50 or above, display Grade D.
  6. Otherwise, display Grade F.

Source Code

#include <stdio.h>

int main()
{
    int marks;

    printf("Enter student's marks: ");
    scanf("%d", &marks);

    if (marks >= 90)
        printf("Grade A");
    else if (marks >= 75)
        printf("Grade B");
    else if (marks >= 60)
        printf("Grade C");
    else if (marks >= 50)
        printf("Grade D");
    else
        printf("Grade F");

    return 0;
}

Sample Output

Enter student's marks: 82
Grade B

Program 3: KYC Verification

Check an identification number against stored KYC records using switch-case.

Source Code

#include <stdio.h>

int main()
{
    long long id;
    int choice;

    long long PAN = 123456;
    long long AADHAR = 987654;
    long long APAAR = 456789;
    long long DL = 112233;
    long long PASSPORT = 778899;

    printf("KYC Verification System\n");
    printf("1. PAN Number\n");
    printf("2. AADHAR Number\n");
    printf("3. APAAR ID\n");
    printf("4. Driving License\n");
    printf("5. Passport\n");

    printf("Select ID Type: ");
    scanf("%d", &choice);

    printf("Enter Identification Number: ");
    scanf("%lld", &id);

    switch(choice)
    {
        case 1:
            if(id == PAN)
                printf("Individual Verified using PAN\n");
            else
                printf("Verification Failed\n");
            break;

        case 2:
            if(id == AADHAR)
                printf("Individual Verified using AADHAR\n");
            else
                printf("Verification Failed\n");
            break;

        case 3:
            if(id == APAAR)
                printf("Individual Verified using APAAR ID\n");
            else
                printf("Verification Failed\n");
            break;

        case 4:
            if(id == DL)
                printf("Individual Verified using Driving License\n");
            else
                printf("Verification Failed\n");
            break;

        case 5:
            if(id == PASSPORT)
                printf("Individual Verified using Passport\n");
            else
                printf("Verification Failed\n");
            break;

        default:
            printf("Invalid ID Type Selected\n");
    }

    return 0;
}

Program 4: Roots of a Quadratic Equation

Source Code

#include <stdio.h>
#include <math.h>

int main()
{
    float a, b, c, discriminant;
    float root1, root2, realPart, imagPart;

    printf("Enter coefficients a, b and c: ");
    scanf("%f %f %f", &a, &b, &c);

    discriminant = b * b - 4 * a * c;

    if(discriminant > 0)
    {
        root1 = (-b + sqrt(discriminant)) / (2 * a);
        root2 = (-b - sqrt(discriminant)) / (2 * a);

        printf("Roots are real and distinct\n");
        printf("Root1 = %.2f\nRoot2 = %.2f", root1, root2);
    }
    else if(discriminant == 0)
    {
        root1 = root2 = -b / (2 * a);

        printf("Roots are real and equal\n");
        printf("Root1 = Root2 = %.2f", root1);
    }
    else
    {
        realPart = -b / (2 * a);
        imagPart = sqrt(-discriminant) / (2 * a);

        printf("Roots are complex\n");
        printf("Root1 = %.2f + %.2fi\n", realPart, imagPart);
        printf("Root2 = %.2f - %.2fi", realPart, imagPart);
    }

    return 0;
}

Program 5: Approximation of sin(x)

Source Code

#include <stdio.h>

int main()
{
    float x, term, sum;
    int n, i;

    printf("Enter the value of x (in radians): ");
    scanf("%f", &x);

    printf("Enter number of terms: ");
    scanf("%d", &n);

    term = x;
    sum = x;

    for(i = 1; i < n; i++)
    {
        term = -term * x * x / ((2 * i) * (2 * i + 1));
        sum = sum + term;
    }

    printf("Approximate value of sin(%.2f) = %.6f", x, sum);

    return 0;
}

Program 6: Keyword Search in Course Description

Source Code

#include <stdio.h>
#include <string.h>

int main()
{
    char description[200];
    char keyword[50];

    printf("Enter course description: ");
    fgets(description, sizeof(description), stdin);

    printf("Enter keyword to search: ");
    scanf("%s", keyword);

    if(strstr(description, keyword) != NULL)
        printf("Keyword '%s' found in the course description.", keyword);
    else
        printf("Keyword '%s' not found in the course description.", keyword);

    return 0;
}

Program 7: Student Pass/Fail and Average

Source Code

#include <stdio.h>

int checkPass(int m1, int m2, int m3)
{
    if(m1 >= 40 && m2 >= 40 && m3 >= 40)
        return 1;
    else
        return 0;
}

int main()
{
    int m1, m2, m3;
    float average;

    printf("Enter marks for three subjects: ");
    scanf("%d %d %d", &m1, &m2, &m3);

    average = (m1 + m2 + m3) / 3.0;

    printf("Average Marks = %.2f\n", average);

    if(checkPass(m1, m2, m3))
        printf("Result: PASS");
    else
        printf("Result: FAIL");

    return 0;
}

Program 8: Swap Two Balances Using Pointers

Source Code

#include <stdio.h>

void swap(float *a, float *b)
{
    float temp;

    temp = *a;
    *a = *b;
    *b = temp;
}

int main()
{
    float balance1, balance2;

    printf("Enter first account balance: ");
    scanf("%f", &balance1);

    printf("Enter second account balance: ");
    scanf("%f", &balance2);

    printf("\nBefore Swapping:\n");
    printf("Balance1 = %.2f\n", balance1);
    printf("Balance2 = %.2f\n", balance2);

    swap(&balance1, &balance2);

    printf("\nAfter Swapping:\n");
    printf("Balance1 = %.2f\n", balance1);
    printf("Balance2 = %.2f\n", balance2);

    return 0;
}

PART – B: TYPICAL OPEN-ENDED EXPERIMENTS

Program 1: Binary Search for Book ID

Source Code

#include <stdio.h>

int binarySearch(int books[], int n, int key)
{
    int low = 0, high = n - 1, mid;

    while(low <= high)
    {
        mid = (low + high) / 2;

        if(books[mid] == key)
            return mid;
        else if(books[mid] < key)
            low = mid + 1;
        else
            high = mid - 1;
    }

    return -1;
}

int main()
{
    int books[100], n, i, key, result;

    printf("Enter number of books: ");
    scanf("%d", &n);

    printf("Enter Book IDs in ascending order:\n");

    for(i = 0; i < n; i++)
        scanf("%d", &books[i]);

    printf("Enter Book ID to search: ");
    scanf("%d", &key);

    result = binarySearch(books, n, key);

    if(result != -1)
        printf("Book found at position %d (index %d)\n", result + 1, result);
    else
        printf("Book not available in the shelf.\n");

    return 0;
}

Program 2: Sort Scores in Descending Order

Source Code

#include <stdio.h>

int main()
{
    int scores[100], n, i, j, temp;

    printf("Enter number of students: ");
    scanf("%d", &n);

    printf("Enter the scores:\n");

    for(i = 0; i < n; i++)
        scanf("%d", &scores[i]);

    for(i = 0; i < n - 1; i++)
    {
        for(j = 0; j < n - i - 1; j++)
        {
            if(scores[j] < scores[j + 1])
            {
                temp = scores[j];
                scores[j] = scores[j + 1];
                scores[j + 1] = temp;
            }
        }
    }

    printf("Scores in descending order:\n");

    for(i = 0; i < n; i++)
        printf("%d ", scores[i]);

    return 0;
}

Program 3: Total Revenue Generated by Each Branch

Source Code

#include <stdio.h>

int main()
{
    int b, p, i, j;

    printf("Enter number of branches: ");
    scanf("%d", &b);

    printf("Enter number of products: ");
    scanf("%d", &p);

    int quantity[b][p];
    int price[p];
    int total[b];

    printf("Enter quantity of products shipped by each branch:\n");

    for(i = 0; i < b; i++)
    {
        printf("Branch %d:\n", i + 1);

        for(j = 0; j < p; j++)
            scanf("%d", &quantity[i][j]);
    }

    printf("Enter revenue per unit for each product:\n");

    for(j = 0; j < p; j++)
        scanf("%d", &price[j]);

    for(i = 0; i < b; i++)
    {
        total[i] = 0;

        for(j = 0; j < p; j++)
            total[i] += quantity[i][j] * price[j];
    }

    printf("\nTotal Revenue Generated by Each Branch:\n");

    for(i = 0; i < b; i++)
        printf("Branch %d: %d\n", i + 1, total[i]);

    return 0;
}

Program 4: Join Names and Calculate Length Without String Functions

Source Code

#include <stdio.h>

int main()
{
    int n, i, j, k;
    int maxWidth = 20;

    printf("Enter number of contacts: ");
    scanf("%d", &n);

    char first[n][50], last[n][50], full[n][100];
    int length[n];

    for(i = 0; i < n; i++)
    {
        printf("\nEnter first name: ");
        scanf("%s", first[i]);

        printf("Enter last name: ");
        scanf("%s", last[i]);

        j = 0;
        k = 0;

        while(first[i][j] != '\0')
        {
            full[i][k] = first[i][j];
            j++;
            k++;
        }

        full[i][k] = ' ';
        k++;

        j = 0;

        while(last[i][j] != '\0')
        {
            full[i][k] = last[i][j];
            j++;
            k++;
        }

        full[i][k] = '\0';

        length[i] = 0;

        while(full[i][length[i]] != '\0')
            length[i]++;
    }

    printf("\nContact List:\n");

    for(i = 0; i < n; i++)
    {
        printf("%s (Length: %d) - ", full[i], length[i]);

        if(length[i] <= maxWidth)
            printf("Fits on screen\n");
        else
            printf("Does NOT fit on screen\n");
    }

    return 0;
}

Program 5: Call by Value and Call by Reference

Source Code

#include <stdio.h>

void simulateSwap(float a, float b)
{
    float temp;

    temp = a;
    a = b;
    b = temp;

    printf("\n[Simulation - Call by Value]");
    printf("\nAfter swap: Currency1 = %.2f, Currency2 = %.2f\n", a, b);
}

void actualSwap(float *a, float *b)
{
    float temp;

    temp = *a;
    *a = *b;
    *b = temp;

    printf("\n[Actual Swap - Call by Reference]");
    printf("\nAfter swap: Currency1 = %.2f, Currency2 = %.2f\n", *a, *b);
}

int main()
{
    float currency1, currency2;

    printf("Enter amount in Currency 1: ");
    scanf("%f", &currency1);

    printf("Enter amount in Currency 2: ");
    scanf("%f", &currency2);

    printf("\nOriginal Values:");
    printf("\nCurrency1 = %.2f, Currency2 = %.2f\n", currency1, currency2);

    simulateSwap(currency1, currency2);

    printf("\nAfter Simulation (Original remains same):");
    printf("\nCurrency1 = %.2f, Currency2 = %.2f\n", currency1, currency2);

    actualSwap(&currency1, &currency2);

    printf("\nAfter Actual Swap (Values updated):");
    printf("\nCurrency1 = %.2f, Currency2 = %.2f\n", currency1, currency2);

    return 0;
}

Program 6: Library Book Details Using Structure

Source Code

#include <stdio.h>

struct Book
{
    char title[100];
    char author[100];
    int year;
};

int main()
{
    int n, i;

    printf("Enter number of books: ");
    scanf("%d", &n);

    struct Book library[n];

    for(i = 0; i < n; i++)
    {
        printf("\nEnter details for Book %d\n", i + 1);

        printf("Title: ");
        scanf(" %[^\n]", library[i].title);

        printf("Author: ");
        scanf(" %[^\n]", library[i].author);

        printf("Year of Publication: ");
        scanf("%d", &library[i].year);
    }

    printf("\n--- Library Book List ---\n");

    for(i = 0; i < n; i++)
    {
        printf("\nBook %d\n", i + 1);
        printf("Title : %s\n", library[i].title);
        printf("Author : %s\n", library[i].author);
        printf("Year : %d\n", library[i].year);
    }

    return 0;
}