-1

I am trying to find the Prime Numbers between 1 and 100 using nested Loops but I am getting weird results.

I think I have a problem with my code but I can't figure out where exactly, can someone help me ?

The first loop that I made will count the numbers from 2 to 100 (i) the second one will count the numbers from 2 to i-1 (j) so when you divide i%j != 0 it should give you the Prime numbers am I right ? thanks a lot for your help

public static void main (String []args){

        for(int i = 2; i<=100 ; i++)
        {
            for(int j = 2 ; j  < i-1 ; j++ )
            {
              if (i%j != 0)
              {
                  System.out.println(i  );      
              }
            }
        }           
}
ITUni121
  • 25
  • 1
  • 3
  • 1
    whoopsie, that's probably about Java, not Javascript :) – pttsky Dec 11 '16 at 22:43
  • 2
    `"the Compiler keeps giving me weird results"` - What does that even mean? What is the actual problem that you're observing? When you debug this, where specifically does it fail? – David Dec 11 '16 at 22:44
  • lets say i ran the program just till 20 , the results are : 5 5 6 7 7 7 7 8 8 8 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 11 11 11 12 12 12 12 12 13 13 13 13 13 13 13 13 13 13 14 14 14 14 14 14 14 14 14 15 15 15 15 15 15 15 15 15 15 16 16 16 16 16 16 16 16 16 16 17 17 17 17 17 17 17 17 17 17 17 17 17 17 18 18 18 18 18 18 18 18 18 18 18 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 20 20 20 20 20 20 20 20 20 20 20 20 – ITUni121 Dec 11 '16 at 22:47
  • @ITUni121: This is a good opportunity to familiarize yourself with the use of a debugger, which is generally the most valuable tool in software development. Running in debug mode, step through the code line by line as it executes and observe the logic. See what the code is actually doing, see the runtime values of the variables, etc. – David Dec 11 '16 at 22:49

3 Answers3

0

so when you divide i%j != 0 it should gives you the Prime numbers am i right ?

No. Only if this condition is true for all j, the number is prime.

Qwertiy
  • 19,681
  • 15
  • 61
  • 128
0

the problem is this

  if (i%j != 0){
                System.out.println(i  );  
                                    }

                }

You are saying, for each value of j, test against, i and if modulus remainder is *not 0, print. However you must check for all iterations of j in the loop before deciding the candidate (i) is prime. *all values of j that must be checked before you can conclude the prime.

Set a boolean flag false (not prime), update the flag to true if (i%j != 0) else false then after your nested for, print against an if with the flag

baku
  • 765
  • 8
  • 22
0

This tests if it's not prime:

if (i%j == 0)

In which case, abort further tests and try the next number, which can be implemented a few ways, but the minimal change to your code is:

outer:
for (int i = 2; i<=100 ; i++) {
    for (int j = 2 ; j < i-1 ; j++ ) {
        if (i%j == 0) {
            continue outer;
        }
    }
    System.out.println(i);      
}

The code outer: is a label (it can be any name, eg foo:), and continue outer; means commence the next iteration of the loop with that label.

Bohemian
  • 412,405
  • 93
  • 575
  • 722