+92 332 4229 857 99ProjectIdeas@Gmail.com

How to generate series of prime numbers (C++)


Prime number series
This example shows how to generate a series of prime number up to the given limit.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"

using namespace std;

int main()
{
bool flag=false;
int limit=0;
int*arr;
int total=0;
int count=0;

cout<<"ENTER THE NUMBER OF TERMS U WANNA GENERATE THE SERIES OF PRIME NUMBERS > ";
cin>>limit;

arr=new int [limit];

for(int num=2;count<limit;num++)
{
flag=false;
for(int i=2;i<num-1;i++)
{
if(num%i==0)
{
flag=true;
break;
}
}
if(flag==false)
{
arr[total++]=num;
count++;
}
}

for(int i=0;i<total;i++)
cout<<i<<"-"<<arr[i]<<endl;

delete arr;

_getche();
return 0;
}

Output
ENTER THE NUMBER OF TERMS U WANNA GENERATE THE SERIES OF PRIME NUMBERS > 9
0-2
1-3
2-5
3-7
4-11
5-13
6-17
7-19
8-23

Related articles

0 comments: