0% found this document useful (0 votes)
14 views2 pages

Xuất Scp - Snt Trên Biên - Số Lần Xuất Hiện

The document contains a C program that prompts the user to input the dimensions of a 2D array and its elements. It identifies and counts prime numbers located on the border of the array, displaying those that appear most frequently. If no prime numbers are found on the border, it notifies the user accordingly.

Uploaded by

Ban Jim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Xuất Scp - Snt Trên Biên - Số Lần Xuất Hiện

The document contains a C program that prompts the user to input the dimensions of a 2D array and its elements. It identifies and counts prime numbers located on the border of the array, displaying those that appear most frequently. If no prime numbers are found on the border, it notifies the user accordingly.

Uploaded by

Ban Jim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Mang 2 chieu n,m < 10

// xuat so nguyen to tren bien, so nguyen to xuat hien nhieu nhat


// xuat so chinh phuong tren bien, so chinh phuong xuat hien nhieu nhat
#include <stdio.h>
#include <math.h>

int prime (int n) {


if (n < 2) return 0;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return 0;
}
return 1;
}

int main () {
int n, m;
printf ("Nhap so hang (n) va cot (m) cua mang: ");
scanf ("%d %d", &n, &m);
if (n <= 0 || n >= 10 || m <= 0 || m >= 10) {
printf ("Nhap sai ! Nhap lai !");
return 1;
}
int a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf ("Nhap phan tu hang (%d) cot (%d): ", i, j);
scanf ("%d", &a[i][j]);
}
}
printf ("\nMang vua tao: ");
for (int i = 0; i < n; i++) {
printf ("\n");
for (int j = 0; j < m; j++) {
printf ("%d ", a[i][j]);
}
}

int prime_num [100];


int prime_count [100];
int count = 0;
printf ("\nCac so nguyen to tren bien la: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == m - 1) {
if (prime(a[i][j])) {
printf (" %d", a[i][j]);
int checked = 0;
for (int k = 0; k < count; k++) {
if (prime_num[k] == a[i][j]) {
prime_count[k]++;
checked = 1;
break;
}
}
if (!checked) {
prime_num[count] = a[i][j];
prime_count[count] = 1;
count++;
}
}
}
}
}

if (count > 0) {
int max = prime_count[0];
for (int i = 1; i < count; i++) {
if (prime_count[i] > max) {
max = prime_count[i];
}
}

printf("\nCac so nguyen to xuat hien nhieu nhat tren bien (%d lan): ", max);
for (int i = 0; i < count; i++) {
if (prime_count[i] == max) {
printf("%d ", prime_num[i]);
}
}
printf("\n");
} else {
printf("Khong co so nguyen to tren bien");
}

return 0;
}

You might also like