21xrx.com
2024-03-29 20:19:28 Friday
登录
文章检索 我的文章 写文章
用于质数的 C++ 程序
2021-07-08 15:14:18 深夜i     --     --
+ +

打印前 n 个素数的 C++ 程序。

C++程序

#include <iostream>
#include <cmath>


using namespace std;

int main()
{
   int n, status = 1, num = 3, count, c;

   cout << "Enter the number of prime numbers to print\n";
   cin >> n;

   if (n >= 1)
   {
      cout << "First " << n <<" prime numbers are :-" << endl;
      cout << 2 << endl;
   }

   for (count = 2; count <=n; )
   {
      for (c = 2; c <= (int)sqrt(num); c++)
      {
         if (num%c == 0)
         {
            status = 0;
            break;
         }
      }
      if (status != 0)
      {
         cout << num << endl;
         count++;
      }
      status = 1;
      num++;
   }        
   
   return 0;
}

 

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复