Home »
C++ Programs
C++ program to find the product of both diagonals of matrix using class
Submitted by Shubh Pachori, on October 03, 2022
Problem statement
Given a matrix, we have to find the product of both diagonals of matrix using a C++ program with the class and object approach.
Example:
Input:
Enter Matrix elements :
[0][0]:1
[0][1]:3
[0][2]:5
[1][0]:7
[1][1]:9
[1][2]:8
[2][0]:6
[2][1]:4
[2][2]:2
Output:
Entered Matrix :
1 3 5
7 9 8
6 4 2
Product of Left diagonal is 18
Product of Right diagonal is 270
C++ code to find the product of both diagonals of matrix using the class and object approach
#include <iostream>
using namespace std;
// create a class
class Matrix {
// private data members
private:
int matrix[3][3];
// public member functions
public:
// getMatrix() function to insert matrix
void getMatrix() {
cout << "Enter Matrix elements :" << endl;
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
cout << "[" << row << "][" << column << "]: ";
cin >> matrix[row][column];
}
}
}
// productDiagonal() function to find
// product of diagonal of matrix
void productDiagonal() {
// initialising variables to perform operations
int row, column, productleft = 1, productright = 1;
cout << "\nEntered Matrix :" << endl;
// nested for loop to show insertted matrix
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
cout << matrix[row][column] << " ";
}
cout << "\n";
}
cout << "\n";
// nested for loop to traverse the whole matrix
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
// if condition for left diagonal
if (row == column) {
productleft = productleft * matrix[row][column];
}
// if condition for right diagonal
if ((row + column) == 2) {
productright = productright * matrix[row][column];
}
}
}
cout << "Product of Left diagonal is " << productleft << endl;
cout << "Product of Right diagonal is " << productright << endl;
}
};
int main() {
// create an object
Matrix M;
// calling getMatrix() function to insert Matrix
M.getMatrix();
// calling productDiagonal() function to find
// product of diagonal of Matrix
M.productDiagonal();
return 0;
}
Output
Enter Matrix elements :
[0][0]: 1
[0][1]: 2
[0][2]: 3
[1][0]: 4
[1][1]: 5
[1][2]: 6
[2][0]: 7
[2][1]: 8
[2][2]: 9
Entered Matrix :
1 2 3
4 5 6
7 8 9
Product of Left diagonal is 45
Product of Right diagonal is 105
Explanation
In the above code, we have created a class Matrix, one int type 2d array data members matrix[3][3] to store the elements of the matrix, and public member functions getMatrix() and productDiagonal() to store the matrix elements and to find the product of both diagonal of matrix.
In the main() function, we are creating an object M of class Matrix, reading the inputted matrix by the user using getMatrix() function, and finally calling the productDiagonal() member function to find the product of both diagonal of the matrix. The productDiagonal() function contains the logic to find the product of both diagonal of the matrix and printing the result.
C++ Class and Object Programs (Set 2) »