Write C Program to interchange diagonals of a matrix



techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. #include <stdio.h>
  2.  
  3. void main ()
  4. {
  5. static int array[10][10];
  6. int i, j, m, n, a;
  7.  
  8. printf("Enter the order of the matix \n");
  9. scanf("%d %d", &m, &n);
  10. if (m == n)
  11. {
  12. printf("Enter the co-efficients of the matrix\n");
  13. for (i = 0; i < m; ++i)
  14. {
  15. for (j = 0; j < n; ++j)
  16. {
  17. scanf("%dx%d", &array[i][j]);
  18. }
  19. }
  20. printf("The given matrix is \n");
  21. for (i = 0; i < m; ++i)
  22. {
  23. for (j = 0; j < n; ++j)
  24. {
  25. printf(" %d", array[i][j]);
  26. }
  27. printf("\n");
  28. }
  29. for (i = 0; i < m; ++i)
  30. {
  31. a = array[i][i];
  32. array[i][i] = array[i][m - i - 1];
  33. array[i][m - i - 1] = a;
  34. }
  35. printf("The matrix after changing the \n");
  36. printf("main diagonal & secondary diagonal\n");
  37. for (i = 0; i < m; ++i)
  38. {
  39. for (j = 0; j < n; ++j)
  40. {
  41. printf(" %d", array[i][j]);
  42. }
  43. printf("\n");
  44. }
  45. }
  46. else
  47. printf("The given order is not square matrix\n");
  48. }
  49.  

Result

C language techstudy.org

     





Related Topics




Popular Posts