Check an input positive interger is a prime number or not
2015-05-13 14:25
316 查看
4000
Compile, Link and Executive:
/* To check an input positive interger is a prime number */ #include<iostream> #include<cmath> using namespace std; bool isPrime(int); int main() { int y; cout<<"Please input a positive interger."<<endl; cin>>y; if (isPrime(y) == 1) cout<<y<<" is a prime number."<<endl; else cout<<y<<" is not a prime number."<<endl; return 0; } bool isPrime(int x) { int i; if(x == 1) { return 0; } else { for(i = 2; i <= floor(sqrt(x)); i++) { if (x % i == 0) { return 0; break; } } if (i > floor(sqrt(x))) { return 1; } } }
Compile, Link and Executive:
kerins-MacBook-Pro:cpp kerin$ ./test4 Please input a positive interger. 1 1 is not a prime number. kerins-MacBook-Pro:cpp kerin$ ./test4 Please input a positive interger. 2 2 is a prime number. kerins-MacBook-Pro:cpp kerin$ ./test4 Please input a positive interger. 3 3 is a prime number. kerins-MacBook-Pro:cpp kerin$ ./test4 Please input a positive interger. 4 4 is not a prime number. kerins-MacBook-Pro:cpp kerin$ ./test4 Please input a positive interger. 88 88 is not a prime number.
相关文章推荐
- 报错解决 unable to unroll loop, loop does not appear to terminate in a timely manner (994 iterations) or unrolled loop is too large, use the [unroll(n)] attribute to force an exact higher number
- How to check if a Number is Positive or Negative in Java - Interview Question
- Check string is palindrome or not
- Bean property 'sessionFactory' is not writable or has an invalid setter method
- This text field does not specify an inputType or a hint
- invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
- This text field does not specify an inputType or a hint
- Bean property 'XXXXX' is not writable or has an invalid setter method
- WCF:Maximum number of items that can be serialized or deserialized in an object graph is '65536'.
- Bean property XX' is not writable or has an invalid setter method
- The input character is not valid in MATLAB statements or expressions.
- 误报的java.sql.SQLException: Parameter number 21 is not an OUT parameter
- 'telnet' is not recognized as an internal or external command
- Android:This text field does not specify an inputType or a hint
- This text field does not specify an inputType or a hint
- Spring配置文件加载时出现Bean property 'newscontentDAO' is not writable or has an invalid setter method错误的解决[00原创]
- Bean property 'loginDAO' is not writable or has an invalid setter method
- “this._postbackSettings.async is null or not an object”问题的解决
- quartz使用时遇到的问题Bean property 'xxx' is not writable or has an invalid setter method. Did you mean 'xxx
- 警告:This text field does not specify an inputType or a hint