-3

My task is to print the first n prime numbers using a CheckPrime function. I managed to do this with one number but what should I do in order for this to work and show me the first 'n' prime numbers?!

Code:

#include <iostream>
using namespace std;

int eprim(int num)
{
  int bec=1,i;
  for(i=2;i<=num/2;i++)
  {
    if(num%1==0)
    {
        bec=0;
        break;
    }
  }
  return bec;
}

int main()
{
  int num,bec,i,n,
  cout<<"intr numarul"<<endl;
  cin>>num;
  bec = eprim(num);
  if (bec==1)
  {
    cout<<"este prim";
  }
  else
  {
    cout<<"Nu este prim";
  }
  return 0;
}
Shrikanth N
  • 652
  • 3
  • 17
Vladut Maican
  • 41
  • 1
  • 5
  • Sounds like you could benefit from one of these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Oct 22 '18 at 16:32
  • Your code could benefit from some more descriptive writing... like using `bool` as the return type of `eprim` and using something like `isPrime` instead of `bec` as a variable name within your function. Also you write *"I managed to do this with one number"* but the only thing that your code could accomplish right now is checking a non-prime number correctly, you didn't provide a solution that could check a single prime number greater than `3` correctly. – grek40 Oct 22 '18 at 16:40
  • 2
    You probably wanted to do `if(num%i==0)`. It probably should be `i` (alphabet) not `1` (number) – Shrikanth N Oct 22 '18 at 16:44

3 Answers3

0

Make a loop, and count until you have the number you want.

int i = 0;
int number = 2;
while(i < n)
{
  if(eprim(number) == ??)
  {
    ++i; // is a prime number
  }
  ++number;
}

I don't know what your function is supposed to return as (num%1==0) is always true.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
0

Your prime checking function is wrong, for each number between 2 and n/2, you test if it divides with 1, which is trivially true for each number. Your for loop inside eprim will always return 0. So your function will say that no number is prime at all.

To correct this issue, inside the prime checking function, you need to check the division for each i, not 1, as follows:

int eprim(int num)
{
    int bec = 1, i;
    for (i = 2; i <= num / 2; i++)
    {
        if (num % i == 0)
        {
            bec = 0;
            break;
        }
    }

    return bec;
}

Now, for checking each number up to a constant read from input, you need another for loop, inside your main function:

int main()
{
    int num, bec, i, n;

    cout << "intr numarul" << endl;
    cin >> num;

    for (i = 2; i <= num; i++)
    {
        bec = eprim(i);
        if (bec == 1)
        {
            cout << "este prim" << endl;
        }
        else
        {
            cout << "nu este prim" << endl;
        }
    }

    return 0;
}

Also note that prime checking can be done in a more efficient way, by checking only the numbers up to (int) sqrt(num) instead of num / 2.

Polb
  • 640
  • 2
  • 8
  • 21
0

Your code should look something like this:

#include <iostream>

using namespace std;

int main()
{
    int prime = 1;
    int counter = 1;
    int numPrimes;
    bool isPrime;

    cout << "How many primes: ";
    cin >> numPrimes;

    while(counter <= numPrimes){
        prime += 1;  
        isPrime = true;
        for(int i = 2; i <= prime/2; i++){
            if(prime % i == 0){
                isPrime = false;
                break;
            }
        }
        if(isPrime){
            cout << prime << endl;
            counter += 1;
        }
    }
}
Little Boy Blue
  • 311
  • 4
  • 17