Prime Numbers Program problem

Ok, so I'm fairly new to c++ and this is my first time posting to this site. My problem is that my program needs to (1) find the first 100 prime numbers (not prime numbers between 1 and 100) and (2) find and print the 10th, 100th, 1000th,..., 1,000,000th primes without print the ones in between in this format:

n: nth Prime:
10 29
100 541
1000 7919
10000 104729
100000 1299709
1000000 15485863

*EDIT*
for some reason, this ^ isn't formatted the way it needs to be in the program so I'll just type it out...it is in columns with the width set to 9.

here is my code so far...i feel like I'm pretty close, but my math is off or something. Instead of returning primes, it returns 2 followed by all odd numbers up to 199 and the part 2 is completely off (because the function isPrime isn't working properly).

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  // Nate Hoover
// Primes
// Sources: 
// This program will find the first 100 primes and the 10, 100, 10000, 100000, 1000000th primes

#include<iostream>
#include<iomanip>
using namespace std;

// function prototype
bool isPrime(int n);  // Returns true if n is a prime, else false 

int main() {
	// declare variables
	int count = 0, current_number = 2;

	// welcome user
	cout << "Welcome! The first 100 prime numbers are:" << endl;

	// find the first 100 prime numbers
	while (count < 100) {
		if (isPrime(current_number)) { // call current number to the isPrime function
			cout << current_number << endl; // print prime numbers to screen
			count++; // add to the counter only if the number is prime
		}
		current_number++; 
	}
	// blank line to seperate parts 1 and 2
	cout << endl;

	// initialize the columns
	cout << setw(9) << left << "n:" << setw(9) << "nth prime:" << endl;

	// set count back to 0
	count = 0;
	// find the primes at the nth position (multiples of 10)
	while (count <= 1000000) {
		if (isPrime(current_number)) { // call current number to the isPrime function
			// switch loop to print nth prime
			switch (count){
			case 10:
				cout << setw(9) << left << count << setw(9) << current_number << endl;
				break;
			case 100:
				cout << setw(9) << left << count << setw(9) << current_number << endl;				
				break;
			case 1000:
				cout << setw(9) << left << count << setw(9) << current_number << endl;	
				break;
			case 10000:
				cout << setw(9) << left << count << setw(9) << current_number << endl;	
				break;
			case 100000:
				cout << setw(9) << left << count << setw(9) << current_number << endl;
				break;
			case 1000000:
				cout << setw(9) << left << count << setw(9) << current_number << endl;
			}
			count++; // add to the counter only if the number is prime
		}
		current_number++;
	}

	// pause and exit
	getchar();
	getchar();
	return 0;
}

// isPrime function to find if number is prime
bool isPrime(int n) {
	for (int i = 2; i < n; i++) {
		if (n % i == 0) {
			return false; // tells the program if the number is not prime
		}
		else {
			return true;
		}
	}
}
Last edited on
Your isPrime() function doesn't actually run the for loop more than once; for i = 2, either n % 2 == 0 and the program immediately returns false (i.e., not prime), which is fine, or n % 2 == 1 and the program immediately returns true (i.e., prime), without checking the other values of i.
Hi @hoovern88

You are really close,
read what Zhuge wrote above,
this example works and
is fairly simple.
(I can't figure out other
way to explain it)

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//PrimeNth.cpp
//##

#include <iostream>
#include <cmath>
using namespace std;

//function prototype
bool isPrime(int n);

int main(){

		int primesCounter=0;
		int number=2;
		while(true){
			if(isPrime(number)){
				++primesCounter;
				cout<<"Prime #"<<primesCounter<<" :"<<number<<endl;
			}//end if
			++number;
			if(primesCounter==100){
				primesCounter=0;
				number=2;
				break;
			}//end if
		}//end while

		cout<<"\n\n";
	
		int nth=10;
		while(true){
                        if(isPrime(number)){
                                ++primesCounter;
				if(primesCounter==nth){
					nth*=10;
                                	cout<<"Prime #"<<primesCounter<<" :"<<number<<endl;
				}//end if
                        }//end if
                        ++number;
                        if(primesCounter==1000000)break;
                           
                }//end while		
	
	//WRITE YOUR PAUSE STATEMENT HERE (if needed) 	
return 0; //indicates success
}//end of main


bool isPrime(int n){
	if(n<2)return false;
	for(int i=2;i<=sqrt(static_cast<double>(n));++i)
		if(n%i==0)return false;

return true;
}//end function isPrime 
Awesome! Thanks for your help guys. finally got it working. working on another program that is a little more advanced that I'm having trouble with...will post a topic soon if you think you can help. It involves math with large numbers (using arrays). I'll explain further in my next post...thanks again!
Topic archived. No new replies allowed.