# Display prime numbers from 100 to 200 in Java

• January 23rd, 2011, 05:46 AM
c.P.u1
Display prime numbers from 100 to 200 in Java
Code Java:

```class primeN   {   public static void main(String args[])     {   int f=1,j; int i; for(i=101;i<=199;i+=2) //System.out.println(i);     {   for(j=3;j<i;j++)       {   if(i%j==0) System.out.println(i); f=0;   } if(f==1) { System.out.println(i); f=1;     } } } }```

There are around 21 prime numbers between 100 and 200... but this source code displays only the numbers 101 and 103... can any1 guide me where did i go wrong in this? my knowledge of programming is confined to if,for,do while, switch statements only...

P.S i am extremely sorry if i have used the wrong section for asking this... :(
• January 23rd, 2011, 06:48 AM
aydea
Re: Display prime numbers from 100 to 200 in Java
Code Java:

```    public class primeN {   public static void main(String[] args) { int f=1,j; int i; for(i=101;i<=199;i+=2) //System.out.println(i); { f=1; for(j=3;j<i;j++){ if(i%j==0){ //System.out.println(i); f=0; break; } } if(f==1){ System.out.println(i); f=1; }   } }   }```

you forget f=1 statement and break in the for loop ;)
• January 23rd, 2011, 07:19 AM
c.P.u1
Re: Display prime numbers from 100 to 200 in Java
aydea bro, thanks a lot.. this program displays exactly 21 prime numbers... i would like to know more about the break statement... what does it actually do? in this example, to which statement does it transfer the control ?
• January 23rd, 2011, 09:35 AM
aydea
Re: Display prime numbers from 100 to 200 in Java
if you want to quit the loop , you use "break". "break" transfer the control at the end of the loop which "break" statement is used.

Code Java:

```for(j=3;j<i;j++){ if(i%j==0){ //System.out.println(i); f=0; break; } } //control is here after break```
• January 24th, 2011, 12:02 AM
DanBrown
Re: Display prime numbers from 100 to 200 in Java
• January 24th, 2011, 01:32 AM
DavidFongs
Re: Display prime numbers from 100 to 200 in Java
While what you have now may work, I would make the following changes:

1. All class names should begin with a capital letter.

2. The variable f should be a boolean, for readability.

3. When checking to see if i%j is 0, you only need to check the values of j from 3 to < i/2. This is basic factoring math.
• January 25th, 2011, 12:27 AM
c.P.u1
Re: Display prime numbers from 100 to 200 in Java
Quote:

Originally Posted by DavidFongs
While what you have now may work, I would make the following changes:

1. All class names should begin with a capital letter.

2. The variable f should be a boolean, for readability.

3. When checking to see if i%j is 0, you only need to check the values of j from 3 to < i/2. This is basic factoring math.

Sorry, but i did not understand the meaning of "f should be a boolean"... though i know the meaning of boolean... cud u plz give me an example... :(
• January 25th, 2011, 03:20 AM
nine05
Re: Display prime numbers from 100 to 200 in Java
Here\'s my sample.
Code Java:

```public class PrimeNo {   public static void main(String[] args) {   for(int i=101;i<=199;i++) { if(i%2!=0 && i%3!=0 && i%5!=0 && i%7!=0 && i%11!=0){ System.out.println(i); } } } }```
• January 25th, 2011, 02:14 PM
DavidFongs
Re: Display prime numbers from 100 to 200 in Java
Quote:

Originally Posted by c.P.u1
Sorry, but i did not understand the meaning of "f should be a boolean"... though i know the meaning of boolean... cud u plz give me an example... :(

Think about it this way. Right now you initialize an int variable to 1, then if you determine the number isn't prime, you assign that int variable to 0. Later in the code, if that int variable is 1, you print out the prime number. This is the only thing you are using that int variable for. Wouldn't it make more sense to have a boolean variable named "isPrime" and set it to true, then if you determine the number isn't prime, set it to false. Finally only printing the number out if isPrime is true.