fork download
  1. //Q77. Check if the elements on the diagonal of a matrix are distinct.
  2. #include <stdio.h>
  3.  
  4. int main() {
  5. int n, i, j, flag = 1;
  6. printf("Enter size of square matrix: ");
  7. scanf("%d", &n);
  8. int arr[n][n];
  9.  
  10.  
  11. printf("Enter matrix elements:\n");
  12. for(i = 0; i < n; i++) {
  13. for(j = 0; j < n; j++) {
  14. scanf("%d", &arr[i][j]);
  15. }
  16. }
  17.  
  18.  
  19. for(i = 0; i < n; i++) {
  20. for(j = i+1; j < n; j++) {
  21. if(arr[i][i] == arr[j][j]) {
  22. flag = 0;
  23. break;
  24. }
  25. }
  26. if(flag == 0)
  27. break;
  28. }
  29.  
  30. if(flag)
  31. printf("All diagonal elements are distinct.\n");
  32. else
  33. printf("Diagonal elements are NOT distinct.\n");
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 5324KB
stdin
3 3
1 2 3
4 5 6
7 8 1
stdout
Enter size of square matrix: Enter matrix elements:
All diagonal elements are distinct.