In this tutorial you will learn about the C Program to Determinant of a Matrix and its application with practical example.
C Program to Determinant of a Matrix
In this tutorial, we will learn to create a C program that will Find the Determinant of a Matrix using C programming.
Prerequisites
Before starting with the tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
What is a Determinant?
The Determinant is a calculated number from the matrix. For calculating the determinant, a matrix needs to be in a square format it means 2X2, 3X3.
Program to Find the Determinant of a Matrix:-
In this tutorial, we will find the Determinant of a Matrix. First, we will take the input elements of the matrix. Then we will print the input matrix to the user. Then we will find the determinant of the matrix form using the arithmetic expression. At last, we will print the output of the matrix to the user.
Below is a C program to find the sum of each row and every column in a matrix.
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <stdio.h>//Including the header files. void main()//body of the main function. { //declaring the required variables for the program. int arry[10][10],i,j,n; int det=0; //Taking the elements of the matrix from the user printf("\n\n Calculate the determinant of a 3 x 3 matrix :\n"); printf("------------------------------------------------------\n"); //Taking the elements of matrix one by one. printf("Input elements in the first matrix :\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("element - [%d],[%d] : ",i,j); scanf("%d",&arry[i][j]); } } // Printing the matrix printf("The matrix is :\n"); for(i=0;i<3;i++) { for(j=0;j<3 ;j++) printf("% 4d",arry[i][j]); printf("\n"); } //Calculating the determinant of the matrix. for(i=0;i<3;i++) det = det + (arry[0][i]*(arry[1][(i+1)%3]*arry[2][(i+2)%3] - arry[1][(i+2)%3]*arry[2][(i+1)%3])); //Printing the determinant of the matrix. printf("\nThe Determinant of the matrix is: %d\n\n",det); } |
Output:-
In the above program, we have first initialized the required variable.
- a[10][10]= it will hold the integer value.
- rw = it will hold the integer value for the loop.
- cl = it will hold the integer value for the loop.
- row = it will hold the input integer value for rows.
- Column = it will hold the input integer value for columns.
Taking the elements of the matrix from the user.
Calculating the Determinant in the matrix using a loop and a mathematical expression.
Printing the Determinant of a matrix.