Ok, so I know lots of people have posted about how to find prime numbers up to a certain number, and I can do that, however If I wanted to output a certain number of prime numbers so that the user inputs say 6, it would out put 2, 3, 5, 7, 11, 13.. not just 2, 3, 5 (up to that number). Any ideas? please help!

kjpwa
0
Newbie Poster

## Recommended Answers

Jump to PostPrimes upto n

`for ( i = 1 ; i < n ; i++ ) { if ( isPrime(i) ) }`

Number of primes

`i = 1 while ( x < n ) { if ( isPrime(i) ) x++ i++ }`

Jump to PostIn other words, every time you output a value, add 1 to a counter. When it reaches you 'magic number' exit the loop.

Jump to Postyou are adding counter twice in your loop, need to remove one.

`void primeTest(int n) { int temp = 2; for (int counter=1; counter<=n; counter++) { cout << "Prime number " << counter << " is " << temp << endl; // counter ++; need to be …`

Jump to PostInstead of this

`if(n<=0) {return 1;} else primeTest(n); return 0;`

do this:

`if (n > 0) { primeTest(n); } return 0;`

It's simple and easy to read.

And:

`for (int counter=1; counter<=n; counter++)`

A

forloop will not give you …

## All 17 Replies

Salem
5,138
Posting Sage

WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague

computerman11
0
Newbie Poster

zyruz
0
Junior Poster in Training

computerman11
0
Newbie Poster

WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague

ANUJ SHROTRIYA
0
Newbie Poster

John A
1,896
Vampirical Lurker
Team Colleague

WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague

Rashakil Fol
978
Super Senior Demiposter
Team Colleague

computerman11
0
Newbie Poster

WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague

computerman11
0
Newbie Poster

WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague

computerman11
0
Newbie Poster

~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster

ANUJ SHROTRIYA
0
Newbie Poster

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.