Photo by Aaron Burden on Unsplash
C program to input a matrix and change the diagonal elements to zero.
Here is a program to convert the diagonal elements to zero.
It cheaks if the element i==j and converts it to zero. And it cheaks if the element i+j+1 == to the number of rows and converts to zero.
Here is the code.
#include<stdio.h>
#include<math.h>
int main(){
printf("C program to initialize/fill all the\n diagonal elements of a matrix with zero and print.\n");
int r,c;
printf("Enter the number of rows in the matrix such that rows=columns:");
scanf("%d",&r);
printf("Enter the number of columns in the matrix:");
scanf("%d",&c);
int arr[r][c];
printf("Enter the elements in the array\n");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
printf("Row %d column %d:",i,j);
scanf("%d",&arr[i][j]);
}
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
printf("%d\t",arr[i][j]);
}
printf("\n");
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(i==j||(i+j+1)==r){
arr[i][j]=0;
}
}
}
printf("The new matrix is :\n");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
printf("%d\t",arr[i][j]);
}
printf("\n");
}
return 0;
}
The main part of the code is.
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(i==j||(i+j+1)==r){
arr[i][j]=0;
}
}
}
Hope you learnt something because of this blog. Dont forget to follow.
Thankyou...