C++ Online Compiler
Example: Determinant of 2x2 Matrix in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Determinant of 2x2 Matrix #include <iostream> using namespace std; int main() { // Step 1: Declare variables to store the matrix elements double a, b, c, d; double determinant; // Step 2: Prompt the user to enter the elements of the 2x2 matrix cout << "Enter the elements of the 2x2 matrix:" << endl; cout << "Element (a): "; cin >> a; cout << "Element (b): "; cin >> b; cout << "Element (c): "; cin >> c; cout << "Element (d): "; cin >> d; // Step 3: Calculate the determinant using the formula (a*d - b*c) determinant = (a * d) - (b * c); // Step 4: Display the input matrix and its calculated determinant cout << "\nThe 2x2 matrix is:" << endl; cout << "[ " << a << " " << b << " ]" << endl; cout << "[ " << c << " " << d << " ]" << endl; cout << "\nThe determinant of the matrix is: " << determinant << endl; return 0; }
Output
Clear
ADVERTISEMENTS