J Ben
J Ben

Reputation: 129

Prime number calculator won't print the integer '2' - Logical Error?

public class CountingPrimes {

public static void main(String[] args) {
    int flag = 0, i, j;
    int count = 0;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the 1st number: ");
    int firstNum = sc.nextInt();

    System.out.println("Enter the 2nd number: ");
    int secondNum = sc.nextInt();
    System.out.println("Counting prime numbers between "
            + firstNum + " and " + secondNum + ":");
    for (i = firstNum; i <= secondNum; i++) {
        for (j = 2; j < i; j++) {
            if (i % j == 0) {
                flag = 0;
                break;
            } else {
                flag = 1;
            }
        }
        if (flag == 1) {
            System.out.println(++count + ". " + i);
        }
    }

}

}

My current output (after the user enters the 2 numbers):

Counting prime numbers between 1 and 20:
1. 3
2. 5
3. 7
4. 11
5. 13
6. 17
7. 19

The number 2 is not being printed, even though it is a prime number. What logical error do I have here that's causing this? Thank you for any help you can offer.

EDIT to Boolean and flag to isPrime

for (i = firstNum; i <= secondNum; i++) {
        Boolean isPrime = true;
        for (j = 2; j < i; j++) {
            if (i % j == 0) {
                isPrime = false;
                break;
            } else {
                isPrime = true;
            }
        }
        if (isPrime == true) {
            System.out.println(++count + ". " + i);
          }
      }
   }
}

Now my output is:

Counting prime numbers between 1 and 20:
1. 1
2. 2
3. 3
4. 5
5. 7
6. 11
7. 13
8. 17
9. 19

Receiving the 2 prime that I had wanted, but now an unwanted '1' has appeared. I've tried changing the initial Boolean value to false or true, but it doesn't work out.

Upvotes: 1

Views: 69

Answers (3)

Patrick87
Patrick87

Reputation: 28312

Your problem now is that you are using a definition of "prime" that works for all prime numbers and most non-prime numbers, but fails for the non-prime number 1. The real definition of a prime number is roughly as follows: a natural number is a prime number if and only if it is evenly divisible by exactly two numbers: one and itself.

You can change your code to match the exact definition precisely:

for (i = firstNum; i <= secondNum; i++) {
        int divisors = 0;
        for (j = 1; j <= i; j++) {
            if (i % j == 0) {
                divisors++;
            }
        }
        if (divisors == 2) {
            System.out.println(++count + ". " + i);
          }
      }
   }
}

Alternatively, you could simply add a check to your iff ((isPrime == true) && (i > 1)).

Upvotes: 1

Bluehorn
Bluehorn

Reputation: 3111

With i = 2 the for j loop is never executed. As flag was initialized to 0 it stays that way and 2 is not reported as prime.

I'd suggest to initialize flag inside the body of the for i loop in front of the inner loop.

Consider renaming flag to isPrime and making it a bool variable.

Upvotes: 0

Cody Richardson
Cody Richardson

Reputation: 157

I believe I see the problem, I think it's in for (j = 2; j < i; j++) {. Since you said j<i, when i is 2, 2<2 is always false, so it will skip the whole for-loop to 3. change the line to for (j = 2; j <= i; j++) { and give it a try.

Upvotes: 0

Related Questions