fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. using namespace std;
  5. int isPrime(int x){
  6. int i;
  7. if(x<2){
  8. return 0;
  9. }
  10. for(i=2;i<=(int)sqrt(x);i++){
  11. if(i!=x){
  12. if(x%i==0){
  13. return 0;
  14. }
  15. }
  16. }
  17. return 1;
  18. }
  19. int main(void) {
  20. string l;//文字列型
  21. cout<<"自然数を入力してください"<<endl;
  22. int a;
  23. cin>>a;
  24. cout<<a<<"以下の素数は以下のとおりです"<<endl;
  25. l="{ ";
  26. for(int i=2;i<=a;i++){
  27. if(isPrime(i)){
  28. l+=to_string(i)+",";
  29. }
  30. }
  31. l.pop_back();
  32. l+=" }";
  33. cout<<l<<endl;
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 16064KB
stdin
100
stdout
自然数を入力してください
100以下の素数は以下のとおりです
{ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97 }