This code is supposed to find the nth prime number (n being input by the user). I have the code to calculate whether a number is prime or not, but I don't know how to get the code to display the nth prime number.

#include <iostream>
using namespace std;

void isprime(int);

int main () 
{
    int x;
    cout << "Which prime number would you like to display? ";
    cin >> x;

    if (x==0)
    {
        cout << "That is not a valid choice. Please enter another number." << endl;
        cin >> x;
    }

    if (x<0)
    {
        cout << "That is not a valid choice. There are no negative prime numbers. Please enter another number." << endl;
        cin >> x;
    }


    return 0;
}

void isprime(int x)
{
    cout << "Enter a number: ";
    int n;
    cin >> n;

    int isprime;

    if(n<2 || n>2 && n%2==0)
    {
        isprime=0;

        for(int i=2; i<n; i++)
        {
            if((n%i)==0)
            {
                isprime=0;
            }
        }
    }

    else isprime=1;
}   

Recommended Answers

All 7 Replies

Start calcumating prime numbers. When you've calculated x of them, there you are...

And your tests for bad values of x would be better if you used a do-while loop.

That's the part I am stuck with. Should I be calculating the prime numbers in my isprime function or main? Also I'm not sure how to write that in the code. Is a do-while loop similar to a while loop? I never heard of do-while loop before. How would that differ from what I have now, with regards to the code?

You should calculate the prime numbers inside of main.
Do-While loops are the same as While loops but Do-While loops go through the loop once no matter what, then checks the condition, ie:

do
{
    // This will run once, even though the condition is false.
}while(1 == 2);

Hope this Helps!

The trouble I am having with the coding is this: My isprime function computes composite numbers as 0 and prime numbers as 1. I want the code to say that when isprime=1, it should count all those until it gets to the x-th value and output that.

Use a counter that increments whenever isprime is 1. Once the counter is equal to the input x, display the value you sent to isprime.

This is my code now, but it always calculates the answer as 0. Where am I going wrong?

#include <iostream>
using namespace std;

int isprime(int&);

int main () 
{
    int i;
    int x;
    cout << "Which prime number would you like to display? ";
    cin >> x;

    if (x==0)
    {
        cout << "That is not a valid choice. Please enter another number." << endl;
        cin >> x;
    }

    if (x<0)
    {
        cout << "That is not a valid choice. There are no negative prime numbers. Please enter another number." << endl;
        cin >> x;
    }

    while (isprime(x)==1)
    {
        for (i=2; i<=x; i++)
        {
            i;
        }
    }

    cout << "The " << x << "th prime number is " << i;

    return 0;
}

int isprime(int&x)
{
    int n;
    int isprime;

    if(n<2 || n>2 && n%2==0)
    {
        isprime==0;

        for(int i=2; i<n; i++)
        {
            if((n%i)==0)
            {
                isprime==0;
            }
        }
    }

    else isprime==1;

    return isprime;
}

You need to think through your problem more carefully, Given any input:
1) How many numbers do you have to test to find x primes? What kind of loop is best for this?
2) How can you count the primes you find so you know when x have been calculated?

And for your input, use while since you know how it works. Right now the user can break your input easily.

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.