Problem Statement:- Program to Find the sum of all diagonal elements of a matrix.
Sample Input/Output:-
Data requirement:-
Input Data:- row_size, col_size, matrix[][]
Output Data:-matrix[][]
Additional Data:- i, j, sum
Program in C
Here is the source code of the C Program to Find the sum of all diagonal elements of a matrix.
Code:
#include<stdio.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the matrix int i,j; printf("Enter the Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } int sum=0; //Calculate sum of the diagonals element for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(i==j) { sum+=matrix[i][j]; } } } /* Another Way to Calculate the sum of the diagonals element for(i=0;i<row_size;i++) sum+=matrix[i][i]; */ //Display the sum of diagonals Element printf("Sum of diagonals Element is: %d",sum); }
Input/Output:
Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 7 9 4 5 Sum of diagonals Element is: 12
Program in C++
Here is the source code of the C++ Program to Find the sum of all diagonal elements of a matrix.
Code:
#include<iostream> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the matrix int i,j; cout<<"Enter the Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } int sum=0; //Calculate sum of the diagonals element for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(i==j) { sum+=matrix[i][j]; } } } /* Another Way to Calculate the sum of the diagonals element for(i=0;i<row_size;i++) sum+=matrix[i][i]; */ //Display the sum of diagonals Element cout<<"Sum of diagonals Element is: "<<sum; }
Input/Output:
Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 5 4 6 7 8 9 3 2 1 Sum of diagonals Element is: 14
Program in Java
Here is the source code of the Java Program to Find the sum of all diagonal elements of a matrix.
Code:
import java.util.Scanner; public class SumOfDiagonals { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the matrix int i,j; System.out.println("Enter the Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } int sum=0; //Calculate sum of the diagonals element for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(i==j) { sum+=matrix[i][j]; } } } /* Another Way to Calculate sum of the diagonals element for(i=0;i<row_size;i++) sum+=matrix[i][i]; */ //Display the sum of diagonals Element System.out.print("Sum of diagonals Element is: "+sum); cs.close(); } }
Input/Output:
Enter the row Size Of the Matrix:4 Enter the columns Size Of the Matrix:4 Enter the Matrix Element: 7 8 9 3 2 5 6 7 1 2 0 5 4 2 1 3 Sum of diagonals Element is: 15Program in Python
Here is the source code of the Python Program to Find the sum of all diagonal elements of a matrix.
Code:
# Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the 1st matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) sum=0 #Calculate sum of the diagonals element for i in range(len(matrix)): for j in range(len(matrix[0])): if i==j: sum+=matrix[i][j] # Display the sum of diagonals Element print("Sum of diagonals Element is: ",sum)
Input/Output:
Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 3 2 0 1 Sum of diagonals Element is: 4
Most Recommend Questions:-
More Questions:-
Array Programming Questions and Solutions(50)
0 Comments
Please do not Enter any spam link in the comment box