/*
Bài 1: In ra man hinh tat ca so nguyen to co 5 chu so sao cho tong cac chu so la so chan.
*/
#include<conio.h>
#include<iostream.h>
// ham kiem tra so nguyen to
int TestSoNT(long a){
if (a<=0)
return 0; // khong phai so nguyen to
for (int i=2;i<a;i++)
if (a%i==0) return 0; // khong phai so nguyen to
return 1; // la so nguyen to
}
// ham chinh
void main(){
clrscr();
cout<<"\n Cac so thoa man dieu kien la: \n";
long n;
for(n=10000;n<=99999;n++)
if (TestSoNT(n)==1)
{
int a1,a2,a3,a4,a5; // so n co dang: a1a2a3a4a5
a5=n%10;
a4=(n/10)%10;
a3=(n/100)%10;
a2=(n/1000)%10;
a1=(n/10000)%10;
if ((a1+a2+a3+a4+a5)%2==0)
cout<<n<<" ; ";
}
getch();
}
//--------------------------------------------------------
//---------------------------------------------------------
/*
Bài 2: Viet cac ham
- Nhap vao day so co n so (3<=n<=30)
- In cac so chan trong day
- Dem so la boi cua 3 va 5
- Sap xep day tang dan
*/
#include<conio.h>
#include<iostream.h>
int a[30],n;
// nhap day
void Nhap(){
cout<<"\n Nhap so phan tu: ";
do{
cout<<"\n n="; cin>>n;
if(n<3||n>30)
cout<<"\n Nhap lai n !";
}while (n<3||n>30);
cout<<"\n Nhap day so: ";
for(int i=0;i<n;i++)
{
cout<<"\n a["<<i<<"]= "; cin>>a[i];
}
}
//In ra cac so chan trong day
void InSoChan(){
cout<<"\n * Cac so chan trong day: \n";
for (int i=0;i<n;i++)
if( a[i]%2==0) cout<<a[i]<<" ; ";
}
// Dem so phan tu la boi cua 3 va 5
int DemSo(){
int dem=0,i=0;
while(i<n){
if(a[i]%3==0&&a[i]%5==0) dem++;
i++;
}
return dem;
}
// Sap xep day tang dan
void SapXep(){
// sap xep Insert_sort
for(int i=1;i<n;i++)
{
int pos=i-1;
int x=a[i];
while(pos>=0&&a[pos]<x){
a[pos+1]=a[pos];
pos--;
}
a[pos+1]=x;
}
// in day da sap xep
cout<<"\n * Day da sap xep: \n";
for(i=0;i<n;i++)
cout<<a[i]<<" ; ";
}
// ham main
void main(){
Nhap();
InSoChan();
cout<<"\n * Dem so la boi cua 3 va 5:"<<DemSo();
SapXep();
getch();
}
/*
Bài 2: Viet cac ham
- Nhap vao day so co n so (3<=n<=30)
- In cac so chan trong day
- Dem so la boi cua 3 va 5
- Sap xep day tang dan
*/
#include<conio.h>
#include<iostream.h>
int a[30],n;
// nhap day
void Nhap(){
cout<<"\n Nhap so phan tu: ";
do{
cout<<"\n n="; cin>>n;
if(n<3||n>30)
cout<<"\n Nhap lai n !";
}while (n<3||n>30);
cout<<"\n Nhap day so: ";
for(int i=0;i<n;i++)
{
cout<<"\n a["<<i<<"]= "; cin>>a[i];
}
}
//In ra cac so chan trong day
void InSoChan(){
cout<<"\n * Cac so chan trong day: \n";
for (int i=0;i<n;i++)
if( a[i]%2==0) cout<<a[i]<<" ; ";
}
// Dem so phan tu la boi cua 3 va 5
int DemSo(){
int dem=0,i=0;
while(i<n){
if(a[i]%3==0&&a[i]%5==0) dem++;
i++;
}
return dem;
}
// Sap xep day tang dan
void SapXep(){
// sap xep Insert_sort
for(int i=1;i<n;i++)
{
int pos=i-1;
int x=a[i];
while(pos>=0&&a[pos]<x){
a[pos+1]=a[pos];
pos--;
}
a[pos+1]=x;
}
// in day da sap xep
cout<<"\n * Day da sap xep: \n";
for(i=0;i<n;i++)
cout<<a[i]<<" ; ";
}
// ham main
void main(){
Nhap();
InSoChan();
cout<<"\n * Dem so la boi cua 3 va 5:"<<DemSo();
SapXep();
getch();
}
0 nhận xét:
Đăng nhận xét