Knowee
Questions
Features
Study Tools

Write a C Program to Compute the Sum of Diagonals of a square matrix. Print the sum of both diagonals separately. Get the number of rows and columns from the user.

Question

Write a C Program to Compute the Sum of Diagonals of a square matrix. Print the sum of both diagonals separately. Get the number of rows and columns from the user.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that computes the sum of diagonals of a square matrix.

#include<stdio.h>

int main() {
    int n, i, j, sum1 = 0, sum2 = 0;
    printf("Enter the number of rows and columns: ");
    scanf("%d", &n);
    int matrix[n][n];
    printf("Enter elements of the matrix: \n");
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            if(i == j) {
                sum1 += matrix[i][j];
            }
            if(i + j == n - 1) {
                sum2 += matrix[i][j];
            }
        }
    }
    printf("Sum of the main diagonal elements is: %d\n", sum1);
    printf("Sum of the secondary diagonal elements is: %d\n", sum2);
    return 0;
}

This program works as follows:

  1. It first asks the user to input the number of rows and columns (which should be the same since it's a square matrix).
  2. It then asks the user to input the elements of the matrix.
  3. It then calculates the sum of the elements in the main diagonal (from top left to bottom right) and the secondary diagonal (from top right to bottom left).
  4. Finally, it prints out the sums of the main and secondary diagonals.

This problem has been solved

Similar Questions

Write a C program that calculates the sum of both diagonals in a square matrix. Implement a function that takes a square matrix and its size as input and returns the sum of the main diagonal and anti-diagonal elements.

Write a program to obtain a matrix and find the sum of its diagonal elements.Note: Only square matrix.Input format :The input consists of the number of rows and columns separated by a space.The second line of the input is matrix elements.Output format :The output prints the sum of diagonal elements.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 31 2 34 5 67 8 9Output 1 :15Input 2 :4 412 23 45 5678 89 98 8765 54 32 2114 25 36 58Output 2 :191

Write a program to obtain a matrix and find the sum of the elements in the lower triangular matrix.Note: Only square matrixInput format :The first line of the input consists of the number of rows and columns.The second line of the input consists of the matrix element.Output format :The output prints the sum of the lower triangular matrix.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 312 23 4556 78 8995 51 20Output 1 :312

Write a program to obtain a matrix and find the sum of each row and each column.Input format :The first line of the input consists of the value of the number of rows and the number of columns.The second line of the input consists of a matrix.Output format :The output prints the sum of each row and each column.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :4 41 2 3 45 6 7 89 10 11 1213 14 15 16Output 1 :Sum of the row 0 = 10Sum of the row 1 = 26Sum of the row 2 = 42Sum of the row 3 = 58Sum of the column 0 = 28Sum of the column 1 = 32Sum of the column 2 = 36Sum of the column 3 = 40Input 2 :3 398 87 6545 32 2845 56 58Output 2 :Sum of the row 0 = 250Sum of the row 1 = 105Sum of the row 2 = 159Sum of the column 0 = 188Sum of the column 1 = 175Sum of the column 2 = 151

Write a C Program to print lower diagonal of a matrix.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.