Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream.h>
- #include<conio.h>
- void nhap(int *n);
- void menu(int n);
- void snt(int n);
- void khung(void);
- void chiahetxandy(int x,int y,int n);//truyen tham so x,y chia het cho x va y
- void chiahetxandkhongchiahety(int x,int y,int n);//truyen tham so x,y chia het cho x va khong chia het y
- void snt1toin(int n);
- int demso(int n);
- void chusnt(int n);
- void tongchusnt(int n);
- void tichchusnt(int n);
- void chusochan(int n);
- void tongchusochan(int n);
- void tichchusochan(int n);
- int main()
- {
- int n;
- nhap(&n);
- menu(n);
- }
- void menu(int n)
- {
- int a;
- do
- {
- cout<<endl<<"ban da nhap n="<<n;
- cout<<endl<<"1: nhap lai n";
- cout<<endl<<"2: kiem tra so nguyen to";
- cout<<endl<<"3: kiem tra cac so tu 1 toi "<<n<<" chia het cho 2 va 3";
- cout<<endl<<"4: kiem tra cac so tu 1 toi "<<n<<" chia het cho 5 va khong chia het cho 8";
- cout<<endl<<"5: kiem tra cac so nguyen to tu 1 toi "<<n;
- cout<<endl<<"6: kiem tra cac chu so la so nguyen to ";
- cout<<endl<<"7: kiem tra tong cac chu so la so nguyen to ";
- cout<<endl<<"8: kiem tra tich cac chu so la so nguyen to ";
- cout<<endl<<"9: kiem tra cac chu so la so chan ";
- cout<<endl<<"10: kiem tra tong cac chu so chan ";
- cout<<endl<<"11: kiem tra tich cac chu so chan ";
- cout<<endl<<"0: thoat";
- cout<<endl<<"ban muon con chuc nang nao: ";cin>>a;
- while(!(cin))
- {
- cin.clear();
- cin.ignore();
- system("cls");
- cout<<"ban da nhap sai hay nhap lai";
- cout<<endl<<"ban da nhap n="<<n;
- cout<<endl<<"1: nhap lai n";
- cout<<endl<<"2: kiem tra so nguyen to";
- cout<<endl<<"3: kiem tra cac so tu 1 toi "<<n<<" chia het cho 2 va 3";
- cout<<endl<<"4: kiem tra cac so tu 1 toi "<<n<<" chia het cho 5 va khong chia het cho 8";
- cout<<endl<<"5: kiem tra cac so nguyen to tu 1 toi "<<n;
- cout<<endl<<"6: kiem tra cac chu so la so nguyen to ";
- cout<<endl<<"7: kiem tra tong cac chu so la so nguyen to ";
- cout<<endl<<"8: kiem tra tich cac chu so la so nguyen to ";
- cout<<endl<<"9: kiem tra cac chu so la so chan ";
- cout<<endl<<"10: kiem tra tong cac chu so chan ";
- cout<<endl<<"11: kiem tra tich cac chu so chan ";
- cout<<endl<<"0: thoat";
- cout<<endl<<"ban muon con chuc nang nao: ";cin>>a;
- }
- switch(a)
- {
- case 1:system("cls");nhap(&n);break;
- case 2:system("cls");khung();snt(n);khung();break;
- case 3:system("cls");chiahetxandy(2,3,n);break;
- case 4:system("cls");chiahetxandkhongchiahety(5,8,n);break;
- case 5:system("cls");snt1toin(n);break;
- case 6:system("cls");chusnt(n);break;
- case 7:system("cls");tongchusnt(n);break;
- case 8:system("cls");tichchusnt(n);break;
- case 9:system("cls");chusochan(n);break;
- case 10:system("cls");tongchusochan(n);break;
- case 11:system("cls");tichchusochan(n);break;
- case 0:exit(0);break;
- }
- if(a<0||a>11)
- {
- system("cls");
- cout<<"ban da nhap sai hay nhap lai";
- }
- }while(true);
- }
- void snt(int n)
- {
- int tam=0;
- for(int i=2;i<=n-1;i++)
- {
- if(n%i==0)
- {
- tam=1;
- break;
- }
- }
- if(tam==0)
- cout<<n<<" la so nguyen to"<<endl;
- else
- cout<<n<<" khong la so nguyen to"<<endl;
- }
- void khung(void)
- {
- cout<<"-------------------------------------------------------------------------------"<<endl;
- }
- void nhap(int *n)
- {
- do
- {
- cout<<"nhap so nguyen n:";
- cin>>*n;
- while(!(cin))
- {
- cin.clear();
- cin.ignore(256,'\n');
- system("cls");
- cout<<"nhap sai hay nhap lai"<<endl;
- cout<<"nhap so nguyen n:";
- cin>>*n;
- }
- if(*n<=0)
- {
- system("cls");
- cout<<"nhap sai hay nhap lai"<<endl;
- }
- }while(*n<=0);
- system("cls");
- }
- void chiahetxandy(int x,int y,int n)
- {
- int tam=0;
- khung();
- cout<<"cac so tu 1 toi "<<n<<" chia het cho "<<x<<" va "<<y<<" la: ";
- for(int i=1;i<=n;i++)
- {
- if(i%x==0&&i%y==0)
- {
- cout<<i<<" ";
- tam=1;
- }
- }
- cout<<endl;
- khung();
- if(tam==0)
- {
- system("cls");
- khung();
- cout<<"khong co so chia het cho "<<x<<" va "<<y<<endl;
- khung();
- }
- }
- void chiahetxandkhongchiahety(int x,int y,int n)
- {
- int tam=0;
- khung();
- cout<<"cac so tu 1 toi "<<n<<" chia het cho "<<x<<" va khong chia het "<<y<<" la: ";
- for(int i=1;i<=n;i++)
- {
- if(i%x==0&&i%y!=0)
- {
- cout<<i<<" ";
- tam=1;
- }
- }
- cout<<endl;
- khung();
- if(tam==0)
- {
- system("cls");
- khung();
- cout<<"khong co so chia het cho "<<x<<" va khong chia het "<<y<<endl;
- khung();
- }
- }
- void snt1toin(int n)
- {
- int tam,tam1=0;
- khung();
- cout<<"cac so nguyen to tu 1 toi "<<n<<" la: ";
- for(int i=1;i<=n;i++)
- {
- tam=0;
- for(int j=2;j<=i-1;j++)
- {
- if(i%j==0)
- tam=1;
- }
- if(tam==0)
- cout<<i<<" ";
- }
- cout<<endl;
- khung();
- }
- int demso(int n)
- {
- int i=n,t=0;
- while(i/10!=0)
- {
- i/=10;
- t++;
- }
- return t+1;
- }
- void chusnt(int n)
- {
- int a,tam,tam1=0;
- a=demso(n);
- khung();
- cout<<"cac chu so la so nguyen to la: ";
- for(int i=1;i<=a;i++)
- {
- tam=0;
- for(int j=2;j<=(n%10)-1;j++)
- {
- if((n%10)%j==0)
- {
- tam=1;
- }
- }
- if(tam==0)
- {
- tam1=1;
- cout<<n%10<<" ";
- }
- n/=10;
- }
- cout<<endl;
- khung();
- if(tam1==0)
- {
- system("cls");
- khung();
- cout<<"khong co chu so nao la so nguyen to"<<endl;
- khung();
- }
- }
- void tongchusnt(int n)
- {
- int a,tam,tam1=0,s=0;
- a=demso(n);
- khung();
- for(int i=1;i<=a;i++)
- {
- tam=0;
- for(int j=2;j<=(n%10)-1;j++)
- {
- if((n%10)%j==0)
- {
- tam=1;
- }
- }
- if(tam==0)
- {
- tam1=1;
- s+=n%10;
- }
- n/=10;
- }
- if(tam1==0)
- {
- system("cls");
- khung();
- cout<<"khong co chu so nao la so nguyen to"<<endl;
- khung();
- }
- else
- {
- system("cls");
- khung();
- cout<<"tong cac chu so la so nguyen to la: "<<s<<endl;
- khung();
- }
- }
- void tichchusnt(int n)
- {
- int a,tam,tam1=0,s=1;
- a=demso(n);
- khung();
- for(int i=1;i<=a;i++)
- {
- tam=0;
- for(int j=2;j<=(n%10)-1;j++)
- {
- if((n%10)%j==0)
- {
- tam=1;
- }
- }
- if(tam==0)
- {
- tam1=1;
- s*=n%10;
- }
- n/=10;
- }
- if(tam1==0)
- {
- system("cls");
- khung();
- cout<<"khong co chu so nao la so nguyen to"<<endl;
- khung();
- }
- else
- {
- system("cls");
- khung();
- cout<<"tich cac chu so la so nguyen to la: "<<s<<endl;
- khung();
- }
- }
- void chusochan(int n)
- {
- int a,s=0;
- a=demso(n);
- for(int i=1;i<=a;i++)
- {
- if((n%10)%2==0)
- s++;
- n/=10;
- }
- if(s==0)
- {
- khung();
- cout<<"khong co chu so chan"<<endl;;
- khung();
- }
- else
- {
- khung();
- cout<<"co "<<s<<" chu so chan"<<endl;;
- khung();
- }
- }
- void tongchusochan(int n)
- {
- int a,s=0;
- a=demso(n);
- for(int i=1;i<=a;i++)
- {
- if((n%10)%2==0)
- s+=n%10;
- n/=10;
- }
- if(s==0)
- {
- khung();
- cout<<"khong co chu so chan"<<endl;;
- khung();
- }
- else
- {
- khung();
- cout<<"tong cac chu so chan la: "<<s<<endl;;
- khung();
- }
- }
- void tichchusochan(int n)
- {
- int a,s=1;
- a=demso(n);
- for(int i=1;i<=a;i++)
- {
- if((n%10)%2==0)
- s*=n%10;
- n/=10;
- }
- if(s==1)
- {
- khung();
- cout<<"khong co chu so chan"<<endl;;
- khung();
- }
- else
- {
- khung();
- cout<<"tich cac chu so chan la: "<<s<<endl;;
- khung();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement