Numbers from 1 to 30 are indivisible by 3

So, I started learning Java just a few days ago, and I am doing great except for one exercise that scares my mind. Thus, the exercise is "Write a program that displays all numbers from 1 to 30 indivisible by 3." So it's easy:

class numbers {
    public static void main (String args[]) {
        for (int i = 0; i <=30; i++){
            switch(i % 3){
                case 0 :
                    break;
                default :
                    System.out.println(i);
                    break;
            }
        }
    }
}

      

Except for one option: "use break

after detecting divisibility by 3. Now I'm not sure if the hte break

used in the above code is correct since it is part switch

. I was wondering if there was another way to do this.

+3


source to share


3 answers


Some fixes:

  • class names must start with a Upper letter, class name Numbers

    , notNumbers

  • start iterating at 1, not 0, because you are showing numbers in the range [1..30].
  • Because here you only have 2 possibilities (are or are not indivisible), replace the switch with an if statement. The switch is more suitable for a wide variety of environments.
  • The most important thing. Using break will force you to exit the loop. Using continue will skip this loop and move on to the next iteration.

So now your code should look shorter and cleaner :)



class Numbers {
    public static void main (String args[]) {
        for (int i = 1; i <=30; i++){
            if(i % 3 == 0){
             continue;
            }
             System.out.println(i);
            }
        }
    }
}

      

Or you can go with a shorter version:

for (int i = 1; i <=30; i++){
    if(i % 3 != 0){
       System.out.println(i);
    }
}

      

+5


source


Another short solution.

As you know, from 1 to 30 there are only 10 numbers divisible by 3, we do ten loops to print them all.

for (int i = 1; i <= 30; ++i) {
    System.out.printf("%d%n%d%n", i++, i++);
}

      

The idea is to print two numbers in front of the one that is divisible by 3 and skip the one that is divisible.



  • counter i

    starts at 1
  • the first one %d

    in System.out.printf

    prints the current one i

    (i = 1) and increments it by 1 (i = 2)
  • the second %d

    in System.out.printf

    prints the current one i

    (i = 2) and increments it by 1 (i = 3)
  • end condition for-loop

    increases i

    by 1 (i = 4)
  • repeat to the end condition i <= 30

    false

edit More readable version (as suggested by ajb)

for (int i = 1; i <= 30; i += 3) {
    System.out.printf("%d%n%d%n", i, i + 1);
}

      

+1


source


The code is fine - if the i % 3

result is 0, nothing is printed. Since there are only two possibilities, I would rewrite the expression as it were:

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

      

Better readable and more clearly conveys intent in my opinion.

More on the actual assertion:

switch( i % 3 )
{
      case 0:
         //do nothing
       break; // leave switch - statement
      ...
}

      

A break is needed to leave the switch statement. So yes, it is necessary.

0


source







All Articles