-8

I'm trying to print all the prime numbers between 2 and 100, but I am getting only 2 and 3.

I've tried every possible alternative, and it comes up with different error or different output.

public static void main(String[] args) {
    boolean flag = true;
    for (int i = 2; i <= 100; i++) {
        for (int j = 2; j < i; j++) {
            if (i % j == 0) {
                flag = false;
                break;
            }
        }

        if (flag) {
            System.out.println(i);
        }
    }
}

I don't need any alternative way, I just want to know what's happening in my code and why it gives only 2 and 3?

Tom
  • 16,842
  • 17
  • 45
  • 54
Trouble Maker
  • 99
  • 1
  • 5
  • 2
    you should reset `flag` back to `true` before the inner loop – Eran Jul 01 '19 at 07:48
  • 2
    Once you get to 4, `flag` gets set to `false`, and you never set it to `true` again. – khelwood Jul 01 '19 at 07:49
  • 2
    It's even better to define `flag` inside the first loop. – Zefick Jul 01 '19 at 07:53
  • 3
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Andy Turner Jul 01 '19 at 07:56
  • @Zabuza: Code Formatting for numbers? Really? With 12k Reputation you should know better. – Tom Jul 01 '19 at 08:12
  • @Tom This is not unusual. There is also a meta discussion about it which ended as personal preference without a clear outcome. Anyways, not a reason to get unfriendly. – Zabuzard Jul 01 '19 at 08:37
  • @Zabuza Do you have a link to that meta discussion? The only one I'm aware of is https://meta.stackoverflow.com/questions/254990/when-should-code-formatting-be-used-for-non-code-text and that neither allows code formatting for text nor is there no clear outcome. – Tom Jul 01 '19 at 08:53

2 Answers2

1

Explanation

You are setting your flag to false once you hit the first non-prime, but you forgot to reset it for the next run.

So after hitting 4, which is not a prime, it is false and stays false.


Solution

You can fix it by just adding a flag = true; to the beginning of your outer loop. Since the flag is only needed inside the loop, you can also just define it directly there:

public static void main(String[] args) {
    for (int i = 2; i <= 100; i++) {
        boolean flag = true; // "Resetting" the flag here
        for (int j = 2; j < i; j++) {
            if (i % j == 0) {
                flag = false;
                break;
            }
        }

        if (flag) {
            System.out.println(i);
        }
    }
}

Notes

The variable name flag is a rather bad name, what about isPrime. That would be pretty idiomatic.

It may also increase readability if you move the check into a dedicated method:

public static boolean isPrime(int number) {
    for (int j = 2; j < number; j++) {
        if (number % j == 0) {
            return false;
        }
    }
    return true;
}

And then use it like that:

for (int i = 2; i <= 100; i++) {
    if (isPrime(i)) {
        System.out.println(i);
    }
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
0

You need to reset the flag back to true. Try this:

                if (i % j == 0) {
                    flag = false;
                    break;
                } else {
                    flag = true;
                }

This gave me the output up to almost 100.

arnonuem
  • 1,317
  • 6
  • 19