fork download
  1. #include <stdio.h>
  2. int main(){
  3. int m, n;
  4. int a[100][100];
  5. printf("nhap so hang m: ");
  6. scanf("%d", &m);
  7. printf("nhap so cot n: ");
  8. scanf("%d", &n);
  9. for(int i=0; i<m; i++){
  10. for(int j=0; j<n; j++){
  11. printf("a[%d][%d] = ", i, j);
  12. scanf("%d", &a[i][j]);
  13. }
  14. }
  15. printf("\nma tran vua nhap:\n");
  16. for(int i = 0; i < m; i++){
  17. for(int j = 0; j < n; j++){
  18. printf("%d\t", a[i][j]);
  19. }
  20. printf("\n");
  21. }
  22. int x;
  23. int found = 0;
  24. printf("\nnhap x can tim: ");
  25. scanf("%d", &x);
  26. for(int i=0; i<m; i++){
  27. for(int j=0; j<n; j++){
  28. if(a[i][j] == x){
  29. found = 1;
  30. break;
  31. }
  32. }
  33. }
  34. if(found){
  35. printf("tim thay %d trong ma tran\n", x);
  36. } else {
  37. printf("Khong tim thay %d\n", x);
  38. }
  39. if(m == n){
  40. int sumChinh = 0, sumPhu = 0;
  41. printf("\ncac phan tu duong cheo chinh: ");
  42. for(int i = 0; i < n; i++){
  43. printf("%d ", a[i][i]);
  44. sumChinh += a[i][i];
  45. }
  46. printf("\ntong duong cheo chinh: %d\n", sumChinh);
  47. printf("cac phan tu duong cheo phu: ");
  48. for(int i = 0; i < n; i++) {
  49. printf("%d ", a[i][n - 1 - i]);
  50. sumPhu += a[i][n - 1 - i];
  51. }
  52. printf("\ntong duong cheo phu: %d\n", sumPhu);
  53. } else {
  54. printf("\nkphai ma tran vuong nen kco duong cheo!\n");
  55. }
  56. return 0;
  57. }
Success #stdin #stdout 0.01s 5352KB
stdin
Standard input is empty
stdout
nhap so hang m: nhap so cot n: 
ma tran vua nhap:

nhap x can tim: Khong tim thay 0

cac phan tu duong cheo chinh: 
tong duong cheo chinh: 0
cac phan tu duong cheo phu: 
tong duong cheo phu: 0