Advertisement
botvn

vậy hoy đi nha

Jan 26th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.03 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void nhap(int *n);
  4. void menu(int n);
  5. void snt(int n);
  6. void khung(void);
  7. void chiahetxandy(int x,int y,int n);//truyen tham so x,y chia het cho x va y
  8. void chiahetxandkhongchiahety(int x,int y,int n);//truyen tham so x,y chia het cho x va khong chia het y
  9. void snt1toin(int n);
  10. int demso(int n);
  11. void chusnt(int n);
  12. void tongchusnt(int n);
  13. void tichchusnt(int n);
  14. void chusochan(int n);
  15. void tongchusochan(int n);
  16. void tichchusochan(int n);
  17. int main()
  18. {
  19.     int n;
  20.     nhap(&n);
  21.     menu(n);
  22. }
  23. void menu(int n)
  24. {
  25.     int a;
  26.     do
  27.     {
  28.         cout<<endl<<"ban da nhap n="<<n;
  29.         cout<<endl<<"1: nhap lai n";
  30.         cout<<endl<<"2: kiem tra so nguyen to";
  31.         cout<<endl<<"3: kiem tra cac so tu 1 toi "<<n<<" chia het cho 2 va 3";
  32.         cout<<endl<<"4: kiem tra cac so tu 1 toi "<<n<<" chia het cho 5 va khong chia het cho 8";
  33.         cout<<endl<<"5: kiem tra cac so nguyen to tu 1 toi "<<n;
  34.         cout<<endl<<"6: kiem tra cac chu so la so nguyen to ";
  35.         cout<<endl<<"7: kiem tra tong cac chu so la so nguyen to ";
  36.         cout<<endl<<"8: kiem tra tich cac chu so la so nguyen to ";
  37.         cout<<endl<<"9: kiem tra cac chu so la so chan ";
  38.         cout<<endl<<"10: kiem tra tong cac chu so chan ";
  39.         cout<<endl<<"11: kiem tra tich cac chu so chan ";
  40.         cout<<endl<<"0: thoat";
  41.         cout<<endl<<"ban muon con chuc nang nao: ";cin>>a;
  42.         while(!(cin))
  43.         {
  44.             cin.clear();
  45.             cin.ignore();
  46.             system("cls");
  47.             cout<<"ban da nhap sai hay nhap lai";
  48.             cout<<endl<<"ban da nhap n="<<n;
  49.             cout<<endl<<"1: nhap lai n";
  50.             cout<<endl<<"2: kiem tra so nguyen to";
  51.             cout<<endl<<"3: kiem tra cac so tu 1 toi "<<n<<" chia het cho 2 va 3";
  52.             cout<<endl<<"4: kiem tra cac so tu 1 toi "<<n<<" chia het cho 5 va khong chia het cho 8";
  53.             cout<<endl<<"5: kiem tra cac so nguyen to tu 1 toi "<<n;
  54.             cout<<endl<<"6: kiem tra cac chu so la so nguyen to ";
  55.             cout<<endl<<"7: kiem tra tong cac chu so la so nguyen to ";
  56.             cout<<endl<<"8: kiem tra tich cac chu so la so nguyen to ";
  57.             cout<<endl<<"9: kiem tra cac chu so la so chan ";
  58.             cout<<endl<<"10: kiem tra tong cac chu so chan ";
  59.             cout<<endl<<"11: kiem tra tich cac chu so chan ";
  60.             cout<<endl<<"0: thoat";
  61.             cout<<endl<<"ban muon con chuc nang nao: ";cin>>a;
  62.         }
  63.         switch(a)
  64.         {
  65.             case 1:system("cls");nhap(&n);break;
  66.             case 2:system("cls");khung();snt(n);khung();break;
  67.             case 3:system("cls");chiahetxandy(2,3,n);break;
  68.             case 4:system("cls");chiahetxandkhongchiahety(5,8,n);break;
  69.             case 5:system("cls");snt1toin(n);break;
  70.             case 6:system("cls");chusnt(n);break;
  71.             case 7:system("cls");tongchusnt(n);break;
  72.             case 8:system("cls");tichchusnt(n);break;
  73.             case 9:system("cls");chusochan(n);break;
  74.             case 10:system("cls");tongchusochan(n);break;
  75.             case 11:system("cls");tichchusochan(n);break;
  76.             case 0:exit(0);break;
  77.         }
  78.         if(a<0||a>11)
  79.         {
  80.             system("cls");
  81.             cout<<"ban da nhap sai hay nhap lai";
  82.         }
  83.     }while(true);
  84.    
  85. }
  86. void snt(int n)
  87. {
  88.     int tam=0;
  89.     for(int i=2;i<=n-1;i++)
  90.     {
  91.         if(n%i==0)
  92.         {
  93.             tam=1;
  94.             break;
  95.         }
  96.     }
  97.     if(tam==0)
  98.     cout<<n<<" la so nguyen to"<<endl;
  99.     else
  100.     cout<<n<<" khong la so nguyen to"<<endl;
  101. }
  102. void khung(void)
  103. {
  104.     cout<<"-------------------------------------------------------------------------------"<<endl;
  105. }
  106. void nhap(int *n)
  107. {
  108.     do
  109.     {
  110.         cout<<"nhap so nguyen n:";
  111.         cin>>*n;
  112.         while(!(cin))
  113.         {
  114.             cin.clear();
  115.             cin.ignore(256,'\n');
  116.             system("cls");
  117.             cout<<"nhap sai hay nhap lai"<<endl;
  118.             cout<<"nhap so nguyen n:";
  119.             cin>>*n;
  120.         }
  121.         if(*n<=0)
  122.         {
  123.             system("cls");
  124.             cout<<"nhap sai hay nhap lai"<<endl;
  125.         }
  126.     }while(*n<=0);
  127.     system("cls");
  128. }
  129. void chiahetxandy(int x,int y,int n)
  130. {
  131.     int tam=0;
  132.     khung();
  133.     cout<<"cac so tu 1 toi "<<n<<" chia het cho "<<x<<" va "<<y<<" la: ";
  134.     for(int i=1;i<=n;i++)
  135.     {
  136.         if(i%x==0&&i%y==0)
  137.         {
  138.             cout<<i<<" ";
  139.             tam=1;
  140.         }
  141.     }
  142.     cout<<endl;
  143.     khung();
  144.     if(tam==0)
  145.     {
  146.         system("cls");
  147.         khung();
  148.         cout<<"khong co so chia het cho "<<x<<" va "<<y<<endl;
  149.         khung();
  150.     }
  151. }
  152. void chiahetxandkhongchiahety(int x,int y,int n)
  153. {
  154.     int tam=0;
  155.     khung();
  156.     cout<<"cac so tu 1 toi "<<n<<" chia het cho "<<x<<" va khong chia het "<<y<<" la: ";
  157.     for(int i=1;i<=n;i++)
  158.     {
  159.         if(i%x==0&&i%y!=0)
  160.         {
  161.             cout<<i<<" ";
  162.             tam=1;
  163.         }
  164.     }
  165.     cout<<endl;
  166.     khung();
  167.     if(tam==0)
  168.     {
  169.         system("cls");
  170.         khung();
  171.         cout<<"khong co so chia het cho "<<x<<" va khong chia het "<<y<<endl;
  172.         khung();
  173.     }
  174. }
  175. void snt1toin(int n)
  176. {
  177.     int tam,tam1=0;
  178.     khung();
  179.     cout<<"cac so nguyen to tu 1 toi "<<n<<" la: ";
  180.     for(int i=1;i<=n;i++)
  181.     {
  182.         tam=0;
  183.         for(int j=2;j<=i-1;j++)
  184.         {
  185.             if(i%j==0)
  186.             tam=1;
  187.         }
  188.         if(tam==0)
  189.         cout<<i<<" ";
  190.     }
  191.     cout<<endl;
  192.     khung();
  193. }
  194. int demso(int n)
  195. {
  196.     int i=n,t=0;
  197.     while(i/10!=0)
  198.     {
  199.         i/=10;
  200.         t++;          
  201.     }
  202.     return t+1;
  203. }
  204. void chusnt(int n)
  205. {
  206.     int a,tam,tam1=0;
  207.     a=demso(n);
  208.     khung();
  209.     cout<<"cac chu so la so nguyen to la: ";
  210.     for(int i=1;i<=a;i++)
  211.     {
  212.         tam=0;
  213.         for(int j=2;j<=(n%10)-1;j++)
  214.         {
  215.             if((n%10)%j==0)
  216.             {
  217.                 tam=1;
  218.             }
  219.            
  220.         }
  221.         if(tam==0)
  222.         {
  223.             tam1=1;
  224.             cout<<n%10<<" ";
  225.         }
  226.         n/=10;
  227.     }
  228.     cout<<endl;
  229.     khung();
  230.     if(tam1==0)
  231.     {
  232.         system("cls");
  233.         khung();
  234.         cout<<"khong co chu so nao la so nguyen to"<<endl;
  235.         khung();
  236.     }
  237. }
  238. void tongchusnt(int n)
  239. {
  240.     int a,tam,tam1=0,s=0;
  241.     a=demso(n);
  242.     khung();
  243.     for(int i=1;i<=a;i++)
  244.     {
  245.         tam=0;
  246.         for(int j=2;j<=(n%10)-1;j++)
  247.         {
  248.             if((n%10)%j==0)
  249.             {
  250.                 tam=1;
  251.             }
  252.            
  253.         }
  254.         if(tam==0)
  255.         {
  256.             tam1=1;
  257.             s+=n%10;
  258.         }
  259.         n/=10;
  260.     }
  261.     if(tam1==0)
  262.     {
  263.         system("cls");
  264.         khung();
  265.         cout<<"khong co chu so nao la so nguyen to"<<endl;
  266.         khung();
  267.     }
  268.     else
  269.     {
  270.         system("cls");
  271.         khung();
  272.         cout<<"tong cac chu so la so nguyen to la: "<<s<<endl;
  273.         khung();
  274.     }
  275. }
  276. void tichchusnt(int n)
  277. {
  278.     int a,tam,tam1=0,s=1;
  279.     a=demso(n);
  280.     khung();
  281.     for(int i=1;i<=a;i++)
  282.     {
  283.         tam=0;
  284.         for(int j=2;j<=(n%10)-1;j++)
  285.         {
  286.             if((n%10)%j==0)
  287.             {
  288.                 tam=1;
  289.             }
  290.            
  291.         }
  292.         if(tam==0)
  293.         {
  294.             tam1=1;
  295.             s*=n%10;
  296.         }
  297.         n/=10;
  298.     }
  299.     if(tam1==0)
  300.     {
  301.         system("cls");
  302.         khung();
  303.         cout<<"khong co chu so nao la so nguyen to"<<endl;
  304.         khung();
  305.     }
  306.     else
  307.     {
  308.         system("cls");
  309.         khung();
  310.         cout<<"tich cac chu so la so nguyen to la: "<<s<<endl;
  311.         khung();
  312.     }
  313. }
  314. void chusochan(int n)
  315. {
  316.     int a,s=0;
  317.     a=demso(n);
  318.     for(int i=1;i<=a;i++)
  319.     {
  320.         if((n%10)%2==0)
  321.         s++;
  322.         n/=10;
  323.     }
  324.     if(s==0)
  325.     {
  326.         khung();
  327.         cout<<"khong co chu so chan"<<endl;;
  328.         khung();
  329.     }
  330.     else
  331.     {
  332.         khung();
  333.         cout<<"co "<<s<<" chu so chan"<<endl;;
  334.         khung();
  335.     }
  336. }
  337. void tongchusochan(int n)
  338. {
  339.     int a,s=0;
  340.     a=demso(n);
  341.     for(int i=1;i<=a;i++)
  342.     {
  343.         if((n%10)%2==0)
  344.         s+=n%10;
  345.         n/=10;
  346.     }
  347.     if(s==0)
  348.     {
  349.         khung();
  350.         cout<<"khong co chu so chan"<<endl;;
  351.         khung();
  352.     }
  353.     else
  354.     {
  355.         khung();
  356.         cout<<"tong cac chu so chan la: "<<s<<endl;;
  357.         khung();
  358.     }
  359. }
  360. void tichchusochan(int n)
  361. {
  362.     int a,s=1;
  363.     a=demso(n);
  364.     for(int i=1;i<=a;i++)
  365.     {
  366.         if((n%10)%2==0)
  367.         s*=n%10;
  368.         n/=10;
  369.     }
  370.     if(s==1)
  371.     {
  372.         khung();
  373.         cout<<"khong co chu so chan"<<endl;;
  374.         khung();
  375.     }
  376.     else
  377.     {
  378.         khung();
  379.         cout<<"tich cac chu so chan la: "<<s<<endl;;
  380.         khung();
  381.     }
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement