fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int a[30], n;
  5.  
  6. void nhapMang() {
  7. do {
  8. printf("Nhập số phần tử của mảng (0 - 30): ");
  9. scanf("%d", &n);
  10. } while (n < 0 || n > 30);
  11.  
  12. for (int i = 0; i < n; i++) {
  13. printf("a[%d] = ", i);
  14. scanf("%d", &a[i]);
  15. }
  16. }
  17.  
  18. void xuatMang() {
  19. printf("Mảng hiện tại: ");
  20. for (int i = 0; i < n; i++) {
  21. printf("%d ", a[i]);
  22. }
  23. printf("\n");
  24. }
  25.  
  26. int socp(int x) {
  27. if (x < 0) return 0;
  28. int can = sqrt(x);
  29. return (can * can == x);
  30. }
  31.  
  32. int sont(int x) {
  33. if (x < 2) return 0;
  34. for (int i = 2; i <= sqrt(x); i++) {
  35. if (x % i == 0) return 0;
  36. }
  37. return 1;
  38. }
  39.  
  40. void timvtphantuchinhphuongmax() {
  41. int max = -1;
  42. int found = 0;
  43.  
  44. for (int i = 0; i < n; i++) {
  45. if (socp(a[i]) && a[i] >= max) {
  46. max = a[i];
  47. found = 1;
  48. }
  49. }
  50.  
  51. if (!found) {
  52. printf("- Mảng không có phần tử là số chính phương.\n");
  53. } else {
  54. printf("- Vị trí phần tử có giá trị là số chính phương lớn nhất: ", max);
  55. for (int i = 0; i < n; i++) {
  56. if (a[i] == max) {
  57. printf("%d ", i);
  58. }
  59. }
  60. printf("\n");
  61. }
  62. }
  63.  
  64. void timphantunnguyentole() {
  65. int found = 0;
  66. printf("- Vị trí các phần tử là số nguyên tố lẻ: ");
  67. for (int i = 0; i < n; i++) {
  68. if (a[i] % 2 == 1 && sont(a[i])) {
  69. printf("%d ", i);
  70. found = 1;
  71. }
  72. }
  73. if (!found) {
  74. printf("Không có phần tử nào thỏa mãn.");
  75. }
  76. printf("\n");
  77. }
  78.  
  79. void tinhTongMang() {
  80. int tong = 0;
  81. for (int i = 0; i < n; i++) {
  82. tong += a[i];
  83. }
  84. printf("- Tổng các phần tử trong mảng là:", tong);
  85. }
  86.  
  87. void sapXepTangDan() {
  88. for (int i = 0; i < n - 1; i++) {
  89. for (int j = i + 1; j < n; j++) {
  90. if (a[i] > a[j]) {
  91. int temp = a[i];
  92. a[i] = a[j];
  93. a[j] = temp;
  94. }
  95. }
  96. }
  97. printf("- Mảng sau khi sắp xếp tăng dần: ");
  98. xuatMang();
  99. }
  100.  
  101. int main() {
  102. nhapMang();
  103. xuatMang();
  104.  
  105. tinhTongMang();
  106. timvtphantuchinhphuongmax();
  107. timphantunnguyentole();
  108. sapXepTangDan();
  109.  
  110. return 0;
  111. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Nhập số phần tử của mảng (0 - 30): Mảng hiện tại: 
- Tổng các phần tử trong mảng là:- Mảng không có phần tử là số chính phương.
- Vị trí các phần tử là số nguyên tố lẻ: Không có phần tử nào thỏa mãn.
- Mảng sau khi sắp xếp tăng dần: Mảng hiện tại: