C Online Compiler
Example: Calculate Determinant of a 2x2 Matrix in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Calculate Determinant of a 2x2 Matrix #include <stdio.h> int main() { // Step 1: Declare variables for matrix elements and the determinant float a, b, c, d; float determinant; // Step 2: Prompt user to enter the elements of the 2x2 matrix printf("Enter the elements of the 2x2 matrix:\n"); printf("Enter element a (top-left): "); scanf("%f", &a); printf("Enter element b (top-right): "); scanf("%f", &b); printf("Enter element c (bottom-left): "); scanf("%f", &c); printf("Enter element d (bottom-right): "); scanf("%f", &d); // Step 3: Display the entered matrix for verification printf("\nYour 2x2 Matrix:\n"); printf("| %.2f %.2f |\n", a, b); printf("| %.2f %.2f |\n", c, d); // Step 4: Calculate the determinant using the formula (ad - bc) determinant = (a * d) - (b * c); // Step 5: Print the calculated determinant printf("\nThe determinant of the matrix is: %.2f\n", determinant); return 0; }
Output
Clear
ADVERTISEMENTS