-->
Showing posts with label 2D Array. Show all posts
Showing posts with label 2D Array. 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 transpose of matrix of order 3x2 | 2D Array-matrix examples

 Array programs collections

/* program to find transpose of given matrix of order 3x2

*/

#include<stdio.h>

int main()

{

int matrix[3][2];

int i,j;

for(i=0;i<=2;i++)

{

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

{

printf("data for %d %d location is:",i,j);

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

}

}

printf("the matrix before transpose is:\n");

for(i=0;i<=2;i++)

{

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

{

printf("%d",matrix[i][j]);

}

printf("\n");

}

printf("the matrix after transpose is:\n");

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

{

for(j=0;j<=2;j++)

{

printf("%d",matrix[j][i]);

}

printf("\n");

}

return 0;

}


Output:

c program to transpose a matrix



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



c program to initialize 2D array | 2D Array examples

2D array programs


 /* program to  initialize 2D array*/

#include<stdio.h>
int main()
{
int a[2][2]={{12,13},{14,15}};
int i,j;
for(i=0;i<=1;i++)//for row
{
for(j=0;j<=1;j++)//for columns
{
printf("%3d",a[i][j]);
//%3d means upto 3 trailing spaces
}
printf("\n");
}
return 0;
}

output:-

program to initialize 2D array