w3resource

C Exercises: Find the sum of left diagonals of a matrix

C Array: Exercise-24 with Solution

Write a program in C to find the sum of the left diagonals of a matrix.

The task is to write a C program that calculates the sum of the elements along the left diagonal of a square matrix. The program prompts the user to input the size of the matrix and its elements, computes the sum of the elements from the top-left to bottom-right diagonal, and displays both the matrix and the sum of its left diagonal elements as output.

Visual Presentation:

C Exercises: Find the sum of left diagonals of a matrix

Sample Solution:

C Code:

#include <stdio.h>

int main() {
    // Declare variables and matrix
    int i, j, arr1[50][50], sum = 0, n, m = 0;

    // Display the purpose of the program
    printf("\n\nFind sum of left diagonals of a matrix :\n");
    printf("---------------------------------------\n");

    // Input the size of the square matrix
    printf("Input the size of the square matrix : ");
    scanf("%d", &n);

    m = n; // Storing the size of matrix for left diagonal traversal

    // Input elements into the matrix
    printf("Input elements in the matrix :\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("element - [%d],[%d] : ", i, j);
            scanf("%d", &arr1[i][j]);
        }
    }

    // Display the matrix
    printf("The matrix is :\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("% 4d", arr1[i][j]);
        }
        printf("\n");
    }

    // Calculate the sum of left diagonals
    for (i = 0; i < n; i++) {
        m = m - 1; // Decrementing to access elements on the left diagonal
        for (j = 0; j < n; j++) {
            if (j == m) {
                sum = sum + arr1[i][j]; // Adding elements on the left diagonal
            }
        }
    }

    // Display the sum of left diagonal elements
    printf("Addition of the left Diagonal elements is : %d\n", sum);

    return 0;
}

Sample Output:

Find sum of left diagonals of a matrix :                                                                      
---------------------------------------                                                                       
Input the size of the square matrix : 2                                                                       
Input elements in the first matrix :                                                                          
element - [0],[0] : 1                                                                                         
element - [0],[1] : 2                                                                                         
element - [1],[0] : 3                                                                                         
element - [1],[1] : 4                                                                                         
The matrix is :                                                                                               
   1   2                                                                                                      
   3   4                                                                                                      
Addition of the  left Diagonal elements is :5

Flowchart:

Flowchart: Find the sum of left diagonals of a matrix.

C Programming Code Editor:

Previous: Write a program in C to find sum of right diagonals of a matrix.
Next: Write a program in C to find the sum of rows an columns of a Matrix.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/array/c-array-exercise-24.php