Program to find the Product of diagonal elements of a matrix
Given an N * N matrix, the task is to find the product of the elements of left and right diagonal.
Examples:
Input: arr[] = 1 2 3 4
5 6 7 8
9 7 4 2
2 2 2 1
Output: 9408
Explanation:
Product of left diagonal = 1 * 4 * 6 * 1 = 24
Product of right diagonal = 4 * 7 * 7 * 2 = 392
Total product = 24 * 392 = 9408
Input: arr[] = 2 1 2 1 2
1 2 1 2 1
2 1 2 1 2
1 2 1 2 1
2 1 2 1 2
Output : 512
Explanation:
Product of left diagonal = 2 * 2 * 2 * 2 * 2 = 32
Product of right diagonal = 2 * 2 * 2 * 2 * 2 = 32
But we have a common element in this case so
Total product = (32 * 32)/2 = 512
Approach:
- We need to find out the principal diagonal and seconddary diagonal elements of the matrix. Please refer this article for this [ Program to print the Diagonals of a Matrix ]
- In this method we use one loop i.e. a loop for calculating product of both the principal and secondary diagonals
- Divide the answer by middle element for matrix of odd size
Below is the implementation of the above approach:
// C++ Program to find the Product // of diagonal elements of a matrix #include <bits/stdc++.h> using namespace std; // Function to find the product of diagonals int productDiagonals(int arr[][100], int n) { int product = 1; // loop for calculating product of both // the principal and secondary diagonals for (int i = 0; i < n; i++) { // For principal diagonal index of row // is equal to index of column product = product * arr[i][i]; // For secondary diagonal index // of column is n-(index of row)-1 product = product * arr[i][n - i - 1]; } // Divide the answer by middle element for // matrix of odd size if (n % 2 == 1) { product = product / arr[n / 2][n / 2]; } return product; } // Driver code int main() { int arr1[100][100] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 7, 4, 2 }, { 2, 2, 2, 1 } }; // Function calling cout << productDiagonals(arr1, 4) << endl; int arr2[100][100] = { { 2, 1, 2, 1, 2 }, { 1, 2, 1, 2, 1 }, { 2, 1, 2, 1, 2 }, { 1, 2, 1, 2, 1 }, { 2, 1, 2, 1, 2 } }; // Function calling cout << productDiagonals(arr2, 5) << endl; return 0; } |
chevron_right
filter_none
Output:
9408 512
Time Complexity: O(N)
Recommended Posts:
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Program to convert the diagonal elements of the matrix to 0
- Find the sum of the diagonal elements of the given N X N spiral matrix
- Program to check diagonal matrix and scalar matrix
- Program to convert given Matrix to a Diagonal Matrix
- Reverse Diagonal elements of matrix
- Squares of Matrix Diagonal Elements
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Maximum product of 4 adjacent elements in matrix
- Program to print elements of a Matrix row-wise skipping alternate elements
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Program to print product of even and odd indexed elements in an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

