Hi guys. I am trying to create a program where the user inputs an integer and the program will cout the nth prime number. Unfortunatly when I try to cout just the nth prime number it doesn't work. However theoretically if I have the program print out all of the prime numbers up to the nth prime number, it just prints out 3,4,5,6,7,8 etc. UI means userinput in my code.

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

int main(){
unsigned int UI; // userinput
int counter1 = 1; // number of primenumbers
bool isprime = true;
int primenumber = 3; //number that it is checking to see if it is prime.
cout << "Enter in a positive integer" << endl;
cin >> UI;
while(UI == 0){
cout << "Invalid input. Please try again" << endl;
cin >> UI;
}
if(UI == 1){
cout << endl << "2"<< endl;
}
else if(UI == 2){
cout << endl << "3" << endl;
}
else if(UI > 2){
while(counter1 != UI){
isprime = true;
for(int checker = 2; checker < primenumber; checker++){
isprime = false;
break;
}
}
if(isprime == true){
counter1++;
}
}
if(counter1 == UI){
}
}

}``````

Actually your algorithm is mostly correct. what you were forgetting is that `2` is the first prime number, but you weren't printing that.

Even though your algorithm is naive, there are some optimizations worth mentioning:

The greatest number that needs to be checked if it's a factor is the square root of the number. Everything that's a factor after that, its reciprocal has already been checked.

After 2 all the other primes are odd. Therefore, you only need to check the odd numbers.

Here's your code, that keeps all this in mind. Plus a few other tweaks:

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

int main()
{
unsigned int UI; // userinput
cout << "Enter in a positive integer" << endl;
cin >> UI;
while (UI == 0)
{
cout << "Invalid input. Please try again" << endl;
cin >> UI;
}
if (UI == 1)
{
cout << endl << "2" << endl;
}
else if (UI == 2)
{
cout << endl << "3" << endl;
}
else if (UI > 2)
{
int counter1 = 1; // number of primenumbers
bool isprime = true;
int primenumber = 3; //number that it is checking to see if it is prime.
cout << 2;
while (counter1 != UI)
{
isprime = true;
for (int checker = 3; checker < limit; checker += 2)
{
if (primenumber % checker == 0)
{
isprime = false;
break;
}
}

if (isprime == true)
{
counter1++;
}
}
cout << '\n';
}
}``````

On a side note. Try to always keep the declaration of your variables as close as possible to where you first need them. On a small project, this is kind of a moot point. But, on a large project it is invaluable.

Just a tip. Many wheels can be had from stock. That is, functions like the test for prime has many ready to use solutions. Bolt that on and run.

Look at the is_prime() function in C at https://rosettacode.org/wiki/AKS_test_for_primes

Rosettcode is a treasure trove of ready to use functions.

great

FYI, there is as small bug in my post the `for` loop should read:

``for (int checker = 3; checker <= limit; checker += 2)``
Be a part of the DaniWeb community

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