-->
Showing posts with label matrix example. Show all posts
Showing posts with label matrix example. Show all posts

Thursday, May 29, 2025

c program to know a matrix is identity or not | matrix examples in c

Array programs collection C

/* to know a matrix of order n is identity or not

1 0 0
0 1 0
0 0 1
*/
#include<stdio.h>
int main()
{
int n,i,j,is_Identity=0;
printf("enter the size of matrix\n");
scanf("%d",&n);
int matrix[n][n];
printf("enter elements of given matrix\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&matrix[i][j]);
}
}
for(i=0;i<=n-1;i++)//loop iteration for row
{
for(j=0;j<=n-1;j++)//loop iteration for column
{
if((i==j && matrix[i][j]!=1)||(i!=j && matrix[i][j]!=0))// checking the condition
// two conditons to bes tested
//Check if the current element is on the diagonal (i == j)
        // In the identity matrix, diagonal elements must be 1
        // Also check if the element is not on the diagonal (i != j)
        // In identity matrix, non-diagonal elements must be 0
is_Identity=1;
break;
}
}
printf("the matrix is:\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%5d",matrix[i][j]);
}
printf("\n");
}
if(is_Identity==0)
{
printf("The matrix is identity");
}
else
{
printf("the matrix is not identity");
}
return 0;
}

Output:-

c program to know a matrix is identity or not



c program to print sum of two matrices of order mxn | 2D Array-matrix examples

Array programs in C 


/* to find sum of two matrix of order mxn

*/

#include<stdio.h>

int main()

{

int m,n,i,j;

printf("enter m as rows and n as columns\n");

scanf("%d%d",&m,&n);//if m=3 then the loop would run for 0,1,2

int m1[m][n],m2[m][n];

printf("enter elements for first matrix m1\n");

for(i=0;i<=m-1;i++)

{

for(j=0;j<=n-1;j++)

{

printf("enter element for location %d %d\n",i,j);

scanf("%d",&m1[i][j]);

}

}

printf("enter elements for second matrix m2\n");

for(i=0;i<=m-1;i++)

{

for(j=0;j<=n-1;j++)

{

printf("enter element for location %d %d\n",i,j);

scanf("%d",&m2[i][j]);

}

}

printf("the sum of two matrices is:\n");

for(i=0;i<=m-1;i++)

{

for(j=0;j<=n-1;j++)

{

printf("%d",m1[i][j]+m2[i][j]);

}

printf("\n");

}

return 0;

}


Output:-

c program to find sum of two matrices



Wednesday, May 28, 2025

c program to print diagonal elements | 2D Array-matrix examples

Array program collection in C


/* program to input elements of a 
matrix of order 3x3 and print only main diagonal elements

 1 2 3
 4 5 6
 7 8 9
 the output would be 1
  5
  6
*/
#include<stdio.h>
int main()
{
int a[3][3];//array/matrix declaration of size 3x3
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("enter element for %d %d location",i,j);
scanf("%d",&a[i][j]);//input for matrix
}
}
for(i=0;i<=2;i++)//it is for row
{
for(j=0;j<=2;j++)//it is for column
{
if(i==j) //condition/logic to print the values
printf("%d",a[i][j]);
else
printf(" "); //printing none if the condition is false
}
printf("\n");
}
return 0;
}

Output:-

c program to print diagonal elements