displaying the first n prime numbers
Oct 16, 2017 at 2:39am UTC
hello i am stuck and need some help. I am trying to make it so it displays the first n prime numbers (starting with 2 and n being user input)to the console but I'm only getting the range 2 through n and i can't seem to figure out how to change it.
code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
using namespace std;
int main() {
int num,i,count,n,isPrime;
cout << "Enter a Number between 30 and 2000: " ;
cin >> n;
count=0;
for (num = 1; count<=n; num++){
isPrime = 0;
count++;
for (i=2;i<=num/2;i++){
if (num%i==0){
isPrime++;
break ;
}
}
if (isPrime==0 && num!= 1)
cout << num << endl;
}
return 0;
}
currently I'm only getting the range. so for example if i put 32 i get primes in the range 2-32. i need the first 32 prime numbers.
Oct 16, 2017 at 2:59am UTC
Oct 16, 2017 at 3:05am UTC
my bad i didn't think the other one posted.
do you know how to delete?
Topic archived. No new replies allowed.