Chào mọi người, mình có đoạn code dưới. mình thử chạy thì không xuất đúng các số nguyên tố và số siêu nguyên tố. Xin mọi người tim giúp lỗi sai với ạ. Tks
#include <iostream>
using namespace std;
#define max 100
void Nhap_Mang(int a[], int n);
void Xuat_Mang(int a[], int n);
bool Kiem_Tra_So_Nguyen_To(int n);
void Xuat_Cac_So_Nguyen_To(int a[], int n);
void Xuat_Cac_So_Sieu_Nguyen_To(int a[], int n);
int main()
{
int a[max];
int n;
do
{
system("cls");
cout << "\nNhap so luong phan tu cua mang: ";
cin >> n;
if (n < 1 || n > 100)
{
cout << "\nNhap sai, moi ban nhap lai\n";
system("pause");
}
} while (n < 1 || n > 100);
cout << "\n\t\t\t\t======NHAP MANG======";
Nhap_Mang(a, n);
cout << "\n\t\t\t\t======XUAT MANG======";
Xuat_Mang(a, n);
cout << "\nCac so nguyen to co trong mang la:";
Xuat_Cac_So_Nguyen_To(a, n);
cout << "\nCac so sieu nguyen to co trong mang la:";
Xuat_Cac_So_Sieu_Nguyen_To(a, n);
system("pause");
return 0;
}
//Nhap 1 mang gom n phan tu
void Nhap_Mang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "\nnhap phan tu a[" << i << "]=";
cin >> a[i];
}
}
//Xuat cac phan tu cua mang da nhap
void Xuat_Mang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "\na[" << i << "]=" << a[i];
}
}
//kiem tra 1 so xem co phai so nguyen to khong
bool Kiem_Tra_So_Nguyen_To(int n)
{
bool KT;
if (n < 2)
{
KT = false;
}
else if (n = 2)
{
KT = true;
}
else if (n > 2)
{
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
KT = false;
break;
}
KT = true;
}
}
return KT;
}
//Xuat cac so nguyen to co trong mang
void Xuat_Cac_So_Nguyen_To(int a[], int n)
{
for (int i = 0; i < n; i++)
{
if (Kiem_Tra_So_Nguyen_To(a[i]) == true)
{
cout << a[i] << " ";
}
}
}
//Xuat các số siêu nguyên tố
void Xuat_Cac_So_Sieu_Nguyen_To(int a[], int n)
{
for (int i = 1; i < n; i++)
{
if (Kiem_Tra_So_Nguyen_To(a[i]) == true)
{
bool KT;
int s = a[i] / 10;
do
{
if (Kiem_Tra_So_Nguyen_To(s) == true)
{
s = s / 10;
KT = true;
}
else
break;
} while (s != 0);
if (KT = true)
{
cout << a[i] << " ";
}
}
}
}