powerunlimited

[HW]1011工程程式設計A班-質數判斷

Nov 19th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4.  
  5.  
  6. int main( )
  7. {
  8.  
  9.     int a=0 , x=2 , y=1 ;
  10.     cin>>a;
  11.  
  12.         int b=sqrt(static_cast<double>(a));
  13.         while(x<=b)
  14.         {
  15.            y=0;
  16.            y=a%x;
  17.            if(y==0)
  18.            break;
  19.            x++;
  20.         }
  21.         if(y==0)
  22.         cout<<"合數"<<endl<<endl;
  23.         else
  24.         cout<<"為質數"<<endl<<endl;
  25.  
  26.  
  27.     system("pause");
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment