I have this code that works for finding a user defined amount of prime numbers.
I however do not completely understand it and was wandering if someone could explain it. Also, how could you write this loop for (int i=2, k=0; k<n; i++) as two seperate loops?
#include <iostream>
usingnamespace std;
int main()
{
int num;
cout << "Enter a number ";
cin >> num;
cout << "The first " << num << " prime numbers are: ";
for (int i=2, k=0; k<n; i++){
for (int j=2; j<i; j++){
if (i%j == 0)
break;
elseif (i == j+1){
cout << i << " ";
k++;
}
}
}
return 0;
}
The problem with changing the for loop for (int i = 2; num--; i++) is that it prints out the prime numbers untill 'num', and not 'num' amount of prime numbers.