Department Launch · Volume 1, Edition 1Filed under: Computer ScienceSponsored by StudentsHQ: Grace Valley CollegeTogether We LearnYou cannot stop innovation
COMPUTER SCIENCEGrace Valley College · Est. 2025
Programs · Output · Practice

C Programming Lab.

Five important programs arranged in a crisp lab archive.

CS2025–2029OFFICIAL
← Go to Home

C Programming Lab

1. Largest Of Three Numbers

#include <stdio.h>

int main() {
    int a, b, c;
    
    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    if (a >= b && a >= c) {
        printf("%d is the largest number.\n", a);
    } else if (b >= a && b >= c) {
        printf("%d is the largest number.\n", b);
    } else {
        printf("%d is the largest number.\n", c);
    }

    return 0;
}
▶ Output
Enter three numbers: 12 45 23
45 is the largest number.

2. Days Into Months And Days

#include <stdio.h>

int main() {
    int total_days, months, days;
    
    printf("Enter total number of days: ");
    scanf("%d", &total_days);

    months = total_days / 30;
    days = total_days % 30;

    printf("%d days = %d Months and %d Days\n", total_days, months, days);

    return 0;
}
▶ Output
Enter total number of days: 100
100 days = 3 Months and 10 Days

3. Root Of 5 Numbers Using Goto Statement

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

int main() {
    int count = 0;
    float num, result;
    
start:
    while(count < 5){
        printf("Enter Number %d : ", count+1);
        scanf("%f", &num);
        if(num <= 0){
            printf("Square Root Of Negative Numbers Are Not Possible !\\n");
        }
        else{
            result = sqrt(num);
            printf("Square Root Of %.2f Is %.2f..\\n", num, result);
        }
        count++;
        goto start;
    }
    printf("Calculation Complete For 5 Numbers !");
}
▶ Output
Enter Number 1 : 25
Square Root Of 25.00 Is 5.00..
Enter Number 2 : -4
Square Root Of Negative Numbers Are Not Possible !
Enter Number 3 : 16
Square Root Of 16.00 Is 4.00..
Enter Number 4 : 2
Square Root Of 2.00 Is 1.41..
Enter Number 5 : 9
Square Root Of 9.00 Is 3.00..
Calculation Complete For 5 Numbers !

4. Sorting Of An Array In Ascending Order

#include <stdio.h>

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

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

    printf("Enter Array elements:");
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

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

    printf("Sorted array in ascending order:\n");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}
▶ Output
Enter number of elements: 5
Enter Array elements:
64 34 25 12 22
Sorted array in ascending order:
12 22 25 34 64

5. Matrix Addition

#include <stdio.h>

int main() {
    int r, c, i, j;
    int a[10][10], b[10][10], sum[10][10];

    printf("Enter the number of rows and columns: ");
    scanf("%d %d", &r, &c);

    printf("Enter elements of 1st matrix:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    printf("Enter elements of 2nd matrix:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            scanf("%d", &b[i][j]);
        }
    }

    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            sum[i][j] = a[i][j] + b[i][j];
        }
    }

    printf("Sum of two matrices:\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            printf("%d   ", sum[i][j]);
        }
        printf("\n");
    }

    return 0;
}
▶ Output
Enter the number of rows and columns: 2 2
Enter elements of 1st matrix:
1 2
3 4
Enter elements of 2nd matrix:
5 6
7 8
Sum of two matrices:
6   8   
10  12