0
 #include<iostream>
using namespace std;
int main(){
int n=5;
int i = 2;
for (i; i <= n; i++)
// for all num to n
{
    int j = 2;
    bool divide = false;
    for (j; j <= n - 1; j++)
    // for checking each num
    {

        if (i % j == 0)
        {
            divide = true;
            break;
        }
        
    }
    if (divide == false)
    {
        cout << i << " ";
       }
     }
  return 0;
   }

my Q is that //please tell me why it is not working //it is expected to give ans 2,3,5 which it is not giving why???

  • 1
    What have you learned by using your debugger? – Stephen Newell Aug 18 '22 at 21:44
  • 6
    You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Aug 18 '22 at 21:46
  • 1
    i don't know how to use debugger – Gagandeep Singh Aug 18 '22 at 21:48
  • 6
    This is a great time to learn, since your program is simple. Once you have the sill down, it'll save you tons of time in the future. – Stephen Newell Aug 18 '22 at 21:50
  • can u help this time i will learn about it later – Gagandeep Singh Aug 18 '22 at 21:51
  • @GagandeepSingh An alternative would be adding print statements everywhere to check out the variables as the flow passes through. I typically use this method instead of the debugger. – Something Something Aug 18 '22 at 22:10
  • 1
    Hey ,I solved it actually I did : for (j; j <=n-1; j++) instead of :for (j; j – Gagandeep Singh Aug 18 '22 at 22:13
  • The nasty thing about programming: Did you really solve it or did you just hide the bug or swap it for a new, unknown bug you'll have to find later? Muhuhahahahahahaha! – user4581301 Aug 18 '22 at 22:23
  • I really solved it by rectifying the error via dry run it is your's approach to hide the bug and bla bla bla................... – Gagandeep Singh Aug 18 '22 at 22:26
  • 2
    FYI, you don't need to test every number. After `2`, all primes are odd. You can increment your index by 2. – Thomas Matthews Aug 19 '22 at 00:16

1 Answers1

0

maybe I found the issue. I think that the problem here is:

for (j; j <= n - 1; j++)

Here you did j<=n-1; So to fix this just do:

for(j; j < i; j++){
    //this should fix

So everything should look like this:

#include<iostream>

using namespace std;

int main() {

int n = 5;
int i = 2;
//check prime numbers starting from i and max n using for loop
for (i = 2; i <= n; i++) {
    bool divide = false;
    for (int j = 2; j < i; j++) {
        if (i % j == 0) {
            divide = true;
            break;
        }
    }
    if (!divide) {
        //!divide is equal to divide=false
        cout << i << " ";
    }
  }
}
Skerdi Velo
  • 121
  • 2
  • 13