C++ Online Compiler
Example: Determinant of a 2x2 Matrix in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Determinant of a 2x2 Matrix #include <iostream> using namespace std; int main() { // Step 1: Declare variables for the four elements of the 2x2 matrix // Using 'double' for flexibility, allowing both integer and decimal inputs. double a, b, c, d; // Step 2: Prompt the user to enter each element of the matrix cout << "Enter the elements of the 2x2 matrix:" << endl; cout << " a b" << endl; cout << " c d" << endl; cout << "Enter element a: "; cin >> a; cout << "Enter element b: "; cin >> b; cout << "Enter element c: "; cin >> c; cout << "Enter element d: "; cin >> d; // Step 3: Calculate the determinant using the formula (ad - bc) double determinant = (a * d) - (b * c); // Step 4: Display the entered matrix and its calculated determinant cout << "\nThe entered 2x2 matrix is:" << endl; cout << "| " << a << " " << b << " |" << endl; cout << "| " << c << " " << d << " |" << endl; cout << "The determinant of the matrix is: " << determinant << endl; return 0; // Indicate successful execution }
Output
Clear
ADVERTISEMENTS