20_10

Write a C program which reads a square matrix a[n][n] and print only the elements that falls in the diagonal starting from a[0][0] to a[n][n] and all other elements printed as 0 (zero).

 Source code:

/*Write a C program which reads a square matrix a[n][n] and print only the elements that falls in the diagonal starting from a[0][0] to a[n][n] and all other elements printed as 0 (zero).*/

#include<stdio.h>

int main()

{

int n, i, j;

int a[10][10];

printf("Enter the number of row = column:");

scanf("%d", &n); /*Number of Rows = Column = n */

printf("Enter the matrix:\n");

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

{

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

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

}

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

{

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

{

if(i!=j)

a[i][j]=0;

}

}

/* printing the matrix in matrix format*/

printf("The diagonal matrix is:\n");

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

{

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

printf("%d\t",a[i][j]); /* print the row elements with one tab space */

printf("\n");

}

return 0;

}

 Output:

C program to make a diagonal matrix


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...