- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Diagonal product of a matrix - JavaScript
Suppose, we have a 2-D array representing a square matrix like this −
const arr = [ [1, 3, 4, 2], [4, 5, 3, 5], [5, 2, 6, 4], [8, 2, 9, 3] ];
We are required to write a function that takes in this array and returns the product of the element present at the principal Diagonal of the matrix.
For this array the elements present at the principal diagonal are −
1, 5, 6, 3
Hence the output should be −
90
Example
Following is the code −
const arr = [ [1, 3, 4, 2], [4, 5, 3, 5], [5, 2, 6, 4], [8, 2, 9, 3] ]; const diagonalProduct = arr => { let product = 1; for(let i = 0; i < arr.length; i++){ for(let j = 0; j < arr[i].length; j++){ if(i === j){ product *= arr[i][j]; }; }; }; return product; }; console.log(diagonalProduct(arr));
Output
Following is the output in the console −
90
- Related Articles
- Convert a single column matrix into a diagonal matrix in R.
- Print matrix in diagonal pattern
- How to create a block diagonal matrix using a matrix in R?
- Program to convert given Matrix to a Diagonal Matrix in C++
- Program to find diagonal sum of a matrix in Python
- Program to print a matrix in Diagonal Pattern.
- Zigzag (or diagonal) traversal of Matrix in C++
- Program to check diagonal matrix and scalar matrix in C++
- Extract the diagonal of a matrix with Einstein summation convention in Python
- Swift Program to Print Diagonal Matrix Pattern
- How to convert a vector into a diagonal matrix in R?
- How to set the diagonal elements of a matrix to 1 in R?
- How to convert diagonal elements of a matrix in R into missing values?
- Matrix product of two arrays in Numpy
- Filling diagonal to make the sum of every row, column and diagonal equal of 3×3 matrix using c++

Advertisements