Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "conio.h"
- #include "math.h"
- int kiemtrasnt(int n)
- {
- for (int i = sqrt(float(n)); i >= 2; i--)
- //tai bat ki gia tri nao cua i ma n chia het thi do khong phai
- //snt nen return 0
- if (n % i == 0) return 0;
- //neu chay xong for thi tuc la n khong chia het cho gia tri i nao ca
- // va neu n > 1 thi la snt hoac khong phai snt neu n <= 1
- if (n > 1) return 1;
- else return 0;
- }
- //ham xoa co tham chieu n vi khi xoa 1 phan tu ta can giam
- //kich thuoc mang xuong. viec truyen tham chieu se thay doi gia tri n trong ham nay
- void xoa(int a[], int k, int &n)
- {
- for (int i = k; i < n - 1; i++)
- {
- a[i] = a[i + 1];
- }
- n--;
- }
- //ham xoa so nugyen to
- void xoant(int a[], int &n)
- {
- for (int i = 0; i < n; i++)
- //kiem tra a[i] co phai snt khong. neu phai thi goi ham xoa de xoa a[i]
- if (kiemtrasnt(a[i]) == 1) xoa(a, i, n);
- }
- void nhapmang(int *a, int n)
- {
- for (int i = 0; i < n; i++)
- {
- printf("nhap phan tu thu %d: ", i + 1);
- scanf("%d.\n", &a[i]);
- }
- }
- void xuatmang(int *a, int n)
- {
- for (int i = 0; i < n; i++)
- printf("%d\t", a[i]);
- }
- int main(void)
- {
- int n, b, k, m;
- printf("Nhap n: ");
- scanf_s("%d.\n", &n);
- int *a = new int[n];
- //nhap gia tri cho mang
- nhapmang(a, n);
- //xuat mang
- xuatmang(a, n);
- //xoa so nguyen to trong mang neu co va xuat mang
- xoant(a, n);
- printf("\nmang sau khi xoa so nguyen to: \n");
- xuatmang(a, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement