Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include<stdio.

h>
#include<conio.h>
void nhap(int &n)
{
do
{
printf("Nhap n=");
scanf("%d",&n);
if(n<0||n>20)
printf("Nhap sai nhap lai");
}while(n<0||n>20);
}
void nhapmang(int a[],int n)
{
for(int i=0;i<n;i++)
{
do
{
printf("Nhap a[%d]",i);
scanf("%d",&a[i]);
if(a[i]>30)
printf("Phan tu lon hon 30 yeu cau nhap lai");
}while(a[i]>30);
}
}
void xuatmang(int a[],int n)
{
for(int i=0;i<n;i++)
printf("%4d",a[i]);
}
int ktsohoanthien(int n)
{
int tonguoc=0;
for(int i=1;i<n;i++)
if(n%i==0)
tonguoc=tonguoc+i;
if(tonguoc==n)
return 1;
else
return 0;
}
void sohoanthien(int a[],int n)
{
int dem=0;
for(int i=0;i<n;i++)
{
if(ktsohoanthien(a[i])==1)
{
dem++;
if(dem==1)
printf("\nCac so hoan thien la\n");
printf("%4d",a[i]);
}
}

if(dem==0)
printf("\nKo co so hoan thien");
}
int ktsonguyento(int n)
{
int uoc=0;
for(int i=1;i<=n;i++)
if(n%i==0)
uoc++;
if(uoc==2)
return 1;
else
return 0;
}
int tongsonguyento(int a[],int n)
{
int tong=0;
for(int i=0;i<n;i++)
if(ktsonguyento(a[i])==1)
tong=tong+a[i];
return tong;
}
void sssonguyento(int a[],int n)
{
int max=0;
for(int i=0;i<n;i++)
{
if(ktsonguyento(a[i])==1&&a[i]>max)
max=a[i];
}
if(max==0)
printf("\nKo co so nguyen to lon nhat");
if(max!=0)
printf("\nSo nguyen to lon nhat la %d",max);
}
void main()
{
clrscr();
int n;
int a[20];
nhap(n);
nhapmang(a,n);
printf("Cac mang da nhap la\n");
xuatmang(a,n);
sohoanthien(a,n);
int t=tongsonguyento(a,n);
if(t!=0)
printf("\nTong cac so nguyen to trong mang la %d",t);
sssonguyento(a,n);
getch();
}

You might also like