BCA

C LAB :PART A

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6 PROGRAM 7 PROGRAM 8 PROGRAM 9 PROGRAM 10

C LAB : PART B

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6 PROGRAM 7 PROGRAM 8 PROGRAM 9 PROGRAM 10

Write a C Program to find the trace of a square matrix using function

    
  /* Write a C Program to find the trace of a square matrix using function */


#include < stdio.h>

#define MAX 100

// User-defined function to calculate trace
int findTrace(int matrix[MAX][MAX], int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += matrix[i][i]; // Diagonal elements satisfy row == column
    }
    return sum;
}

int main() {
    int n;
    int matrix[MAX][MAX];

    printf("Enter the order of the square matrix (N x N): ");
    if (scanf("%d", &n) != 1 || n <= 0 || n > MAX) {
        printf("Invalid matrix size.\n");
        return 1;
    }

    printf("Enter the elements of the %dx%d matrix:\n", n, n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    int trace = findTrace(matrix, n);

    printf("\nTrace of the matrix = %d\n", trace);

    return 0;
}