-6

I am tasked with creating a program that outputs the first N prime numbers. The user inputs the value N. Here is my current program. It may look weirdly formatted but I don't know how to fix that. It's pretty easy to see what is happening though.

#include <iostream>
using namespace std;

int main() {

    cout << "How many prime numbers?";
    int N;
    cin >> N;

    if(N=1){
        cout << "2";
    }

    if(N>1){
        cout << "2" << "\n";
        int i=N-1; //i=prime counters
        int j=3; //j=test prime
        do {
            int k;
            for(k=2; k<j;){ //divisibility test
                if(j%k!=0 and k!=j-1){ //indivisible, check next divisor
                    k=k+1;
                    continue;
                }
                if(j%k!=0 and k==j-1){ //indivisible and last divisor, display prime
                    cout << j << "\n";
                    i=i-1 //removes a prime counter
                    break;
                }
                if(j%k==0){ // divisible, break from loop
                    break;
                }
            }
            j=j+1; //test next prime
        } while(i>0); //will not continue printing primes if number has exceeded N
    }
    return 0;
}

The program outputs "2" no matter what. Then, it tests each number for divisibility with every number before it except for 1. If it is indivisible and still has divisors to get through, it continues. If it is indivisible and has no more divisors left, it is printed, and one "prime counter" is taken away. If it is divisible, the loop is broken and it moves on to test the next number. When the prime counters reach 0, the do while loop ends and the program is complete.

I reached an issue with the program when it outputted the following:

How many prime numbers? 2

I have no idea why this is happening since I specified N as an integer and asked for the user to input N. It doesn't even give me the opportunity to input N, it just automatically prints "2". What is going on?

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
David Plotnik
  • 27
  • 1
  • 2
  • 6
    `if(N=1)` This assigns `1` to `N`, it doesn't compare `N` with `1`. The condition is always true. Consult your favorite C++ textbook about a difference between `=` and `==` – Igor Tandetnik Dec 20 '18 at 05:39
  • Totally unrelated: You may find prime number sieving algorithms quicker than your current approach. – user4581301 Dec 20 '18 at 06:05

1 Answers1

2

You had a few basic errors in your code. You can see a working version at

http://coliru.stacked-crooked.com/a/3e6ab22ca6e15b00

The main error you had was

if(N=1){
    cout << "2";
}

you meant to write

if(N==1){
    cout << "2";
}

Note the equality checking is with == and assignment is with =. Any decent compiler will tell you you are using it wrong. For example you should see a warning on your compiler output like

main.cpp: In function 'int main()':
main.cpp:8:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
     if(N=1){
        ~^~

But in general the code works.

#include <iostream>
using namespace std;

int main() {

   int N = 10;

   if(N=1){
    cout << "2";
   }

    if(N>1){
        cout << "2" << "\n";
        int i=N-1; //i=prime counters
        int j=3; //j=test prime
        do {
            int k;
            for(k=2; k<j;){ //divisibility test
                if(j%k!=0 and k!=j-1){ //indivisible, check next divisor
                    k=k+1;
                    continue;
                }
                if(j%k!=0 and k==j-1){ //indivisible and last divisor, display prime
                    cout << j << "\n";
                    i=i-1; //removes a prime counter
                    break;
                }
                if(j%k==0){ // divisible, break from loop
                    break;
                }
            }
            j=j+1; //test next prime
        } while(i>0); //will not continue printing primes if number has exceeded N
    }
    return 0;
}

and the output is

2
3
5
7
11
13
17
19
23
29

You may also want to change your code to remove the

using namespace std;

It is generally considered bad practise. More info here

bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217