Write a program to check whether the diagonal elements of a (4*4) matrix are all Zero.
#include <stdio.h> #define SIZE 4 int main(){ int matrix[SIZE][SIZE], i, j; // Read the data into the matrix printf("Enter Matrix Data: \n"); for(i=0; i < SIZE; i++){ printf("Enter Data of row %d:\n", i+1); for(j=0; j < SIZE; j++){ scanf("%d", &matrix[i][j] ); } } /** Check If Disagonal Element are same*/ i = matrix[0][0]; j = 0; while(j < SIZE ){ if(matrix[j][j] != i){ break; }else{ j++; } } //if j is equal to the size of the matrix then voila! diagonal entries match. if(j == SIZE){ printf("The diagonal is the same\n"); }else{ printf("The diagonal is not the same. %d, %d\n"); } return 0; }
Click here to submit your answer.