C++ Online Compiler
Example: Determinant of 3x3 Matrix in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Determinant of 3x3 Matrix #include <iostream> using namespace std; int main() { // Step 1: Declare and initialize a 3x3 matrix // You can also take input from the user here. int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Step 2: Print the matrix for verification cout << "The 3x3 Matrix is:" << endl; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << matrix[i][j] << " "; } cout << endl; } // Step 3: Calculate the determinant using the formula // For a matrix: // | a b c | // | d e f | // | g h i | // Determinant = a(ei - fh) - b(di - fg) + c(dh - eg) int det = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) - matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) + matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]); // Step 4: Display the result cout << "\nDeterminant of the 3x3 matrix is: " << det << endl; return 0; }
Output
Clear
ADVERTISEMENTS