Final Practical File

S.No. Problem Statement Date Sign
1 Program to declare, initialize, and display a one-dimensional array
2 Program to find the average of all elements in an array
3 Program to find the largest and smallest elements in an array
4 Program to reverse an array
5 Program to delete an element from an array at a given position
6 Program to insert an element in an array at a specific position
7 Program to count even and odd numbers in an array
8 Program to copy elements from one array to another
9 Program to sort an array using Bubble Sort
10 Program to remove duplicate elements from an array
11 Program to count the number of prime numbers in an array
12 Program to input and display a two-dimensional array
13 Program to find the row-wise sum of a two-dimensional array
14 Program to find the column-wise sum of a two-dimensional array
15 Program to count even and odd elements in a matrix
16 Program to print the diagonal elements of a matrix
17 Program to sort each row of a matrix in ascending order
18 Program to sort each column of a matrix in ascending order
19 Program to input and display elements of a three-dimensional array
20 Program to print a three-dimensional array slice-wise

1. Program to declare, initialize, and display a one-dimensional array


#include <stdio.h>
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    printf("Array elements: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Output:

Array elements: 1 2 3 4 5

2. Program to find the average of all elements in an array


#include <stdio.h>
int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int sum = 0;
    for (int i = 0; i < 5; i++) {
        sum += arr[i];
    }
    float avg = (float)sum / 5;
    printf("Average: %.2f", avg);
    return 0;
}

Output:

Average: 30.00

3. Program to find the largest and smallest elements in an array


#include <stdio.h>
int main() {
    int arr[5] = {5, 3, 9, 1, 7};
    int max = arr[0], min = arr[0];
    for (int i = 1; i < 5; i++) {
        if (arr[i] > max) max = arr[i];
        if (arr[i] < min) min = arr[i];
    }
    printf("Largest: %d\nSmallest: %d", max, min);
    return 0;
}

Output:

Largest: 9
Smallest: 1

4. Program to reverse an array


#include <stdio.h>
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    printf("Reversed array: ");
    for (int i = 4; i >= 0; i--) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Output:

Reversed array: 5 4 3 2 1

5. Program to delete an element from an array at a given position


#include <stdio.h>
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int pos = 2; // Position to delete
    for (int i = pos; i < 4; i++) {
        arr[i] = arr[i + 1];
    }
    printf("Array after deletion: ");
    for (int i = 0; i < 4; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Output:

Array after deletion: 1 2 4 5

6. Program to insert an element in an array at a specific position


#include <stdio.h>
int main() {
    int arr[6] = {1, 2, 3, 4, 5};
    int pos = 2, element = 10;
    for (int i = 5; i > pos; i--) {
        arr[i] = arr[i - 1];
    }
    arr[pos] = element;
    printf("Array after insertion: ");
    for (int i = 0; i < 6; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Output:

Array after insertion: 1 2 10 3 4 5

7. Program to count even and odd numbers in an array


#include <stdio.h>
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int even = 0, odd = 0;
    for (int i = 0; i < 5; i++) {
        if (arr[i] % 2 == 0) even++;
        else odd++;
    }
    printf("Even: %d\nOdd: %d", even, odd);
    return 0;
}

Output:

Even: 2
Odd: 3

8. Program to copy elements from one array to another


#include <stdio.h>
int main() {
    int arr1[5] = {1, 2, 3, 4, 5};
    int arr2[5];
    for (int i = 0; i < 5; i++) {
        arr2[i] = arr1[i];
    }
    printf("Copied array: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr2[i]);
    }
    return 0;
}

Output:

Copied array: 1 2 3 4 5

9. Program to sort an array using Bubble Sort


#include <stdio.h>
int main() {
    int arr[5] = {5, 3, 4, 1, 2};
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    printf("Sorted array: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Output:

Sorted array: 1 2 3 4 5

10. Program to remove duplicate elements from an array


#include <stdio.h>
int main() {
    int arr[10] = {1, 2, 2, 3, 4, 4, 5, 6, 6, 7};
    int n = 10;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n;) {
            if (arr[j] == arr[i]) {
                for (int k = j; k < n - 1; k++) {
                    arr[k] = arr[k + 1];
                }
                n--;
            } else {
                j++;
            }
        }
    }
    printf("Array after removing duplicates: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Output:

Array after removing duplicates: 1 2 3 4 5 6 7

11. Program to count the number of prime numbers in an array


#include <stdio.h>
int isPrime(int num) {
    if (num <= 1) return 0;
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) return 0;
    }
    return 1;
}
int main() {
    int arr[5] = {2, 3, 4, 5, 6};
    int count = 0;
    for (int i = 0; i < 5; i++) {
        if (isPrime(arr[i])) count++;
    }
    printf("Number of prime numbers: %d", count);
    return 0;
}

Output:

Number of prime numbers: 3

12. Program to input and display a two-dimensional array


#include <stdio.h>
int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    printf("2D Array:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Output:

2D Array:
1 2 3
4 5 6

13. Program to find the row-wise sum of a two-dimensional array


#include <stdio.h>
int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    for (int i = 0; i < 2; i++) {
        int sum = 0;
        for (int j = 0; j < 3; j++) {
            sum += arr[i][j];
        }
        printf("Sum of row %d: %d\n", i + 1, sum);
    }
    return 0;
}

Output:

Sum of row 1: 6
Sum of row 2: 15

14. Program to find the column-wise sum of a two-dimensional array


#include <stdio.h>
int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    for (int j = 0; j < 3; j++) {
        int sum = 0;
        for (int i = 0; i < 2; i++) {
            sum += arr[i][j];
        }
        printf("Sum of column %d: %d\n", j + 1, sum);
    }
    return 0;
}

Output:

Sum of column 1: 5
Sum of column 2: 7
Sum of column 3: 9

15. Program to count even and odd elements in a matrix


#include <stdio.h>
int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int even = 0, odd = 0;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            if (arr[i][j] % 2 == 0) even++;
            else odd++;
        }
    }
    printf("Even: %d\nOdd: %d", even, odd);
    return 0;
}

Output:

Even: 2
Odd: 4

16. Program to print the diagonal elements of a matrix


#include <stdio.h>
int main() {
    int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    printf("Diagonal elements: ");
    for (int i = 0; i < 3; i++) {
        printf("%d ", arr[i][i]);
    }
    return 0;
}

Output:

Diagonal elements: 1 5 9

17. Program to sort each row of a matrix in ascending order


#include <stdio.h>
int main() {
    int arr[3][3] = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 3 - j - 1; k++) {
                if (arr[i][k] > arr[i][k + 1]) {
                    int temp = arr[i][k];
                    arr[i][k] = arr[i][k + 1];
                    arr[i][k + 1] = temp;
                }
            }
        }
    }
    printf("Sorted matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Output:

Sorted matrix:
1 2 3
4 5 6
7 8 9

18. Program to sort each column of a matrix in ascending order


#include <stdio.h>
int main() {
    int arr[3][3] = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
    for (int j = 0; j < 3; j++) {
        for (int i = 0; i < 3; i++) {
            for (int k = 0; k < 3 - i - 1; k++) {
                if (arr[k][j] > arr[k + 1][j]) {
                    int temp = arr[k][j];
                    arr[k][j] = arr[k + 1][j];
                    arr[k + 1][j] = temp;
                }
            }
        }
    }
    printf("Sorted matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Output:

Sorted matrix:
3 2 1
6 5 4
9 8 7

19. Program to input and display elements of a three-dimensional array


#include <stdio.h>
int main() {
    int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
    printf("3D Array:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                printf("%d ", arr[i][j][k]);
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

Output:

3D Array:
1 2
3 4

5 6
7 8

20. Program to print a three-dimensional array slice-wise


#include <stdio.h>
int main() {
    int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
    printf("Slice-wise 3D Array:\n");
    for (int i = 0; i < 2; i++) {
        printf("Slice %d:\n", i + 1);
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                printf("%d ", arr[i][j][k]);
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

Output:

Slice-wise 3D Array:
Slice 1:
1 2
3 4

Slice 2:
5 6
7 8

21. Program to input and display elements of a three-dimensional array slice-wise


#include <stdio.h>

int main() {
    int arr[2][3][3] = {
        {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
        {{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}
    };

    printf("3D Array Slice-wise:\n");
    for (int i = 0; i < 2; i++) {
        printf("Slice %d:\n", i + 1);
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 3; k++) {
                printf("%d ", arr[i][j][k]);
            }
            printf("\n");
        }
        printf("\n");
    }

    return 0;
}

Output:

3D Array Slice-wise:
Slice 1:
1 2 3
4 5 6
7 8 9

Slice 2:
10 11 12
13 14 15
16 17 18