Program that prints out repetitions of 3 and 5

Posted on

Problem

This works in my Eclipse, but I think there is a better way to do it.

public static void main(String args[]) {

       for(int i = 1; i < 101; i++)
       {
           if(i%3 == 0)
           {
           System.out.println("repetitive of 3");
           }

           else if(i%5 == 0)
           {
           System.out.println("repetitive of 5");
           }

           else{
               System.out.println(i);
           }


       }


   }

}

Solution

Moderation Note: This answer was being typed at the same time as the asker changed his code, to avoid this bug. Here’s the code, as it was, when this answer was typed:

Program that prints out repetitives of 3,5 and both of them

public static void main(String args[]) {

       for(int i = 1; i < 101; i++)
       {
           if(i%3 == 0)
           {
           System.out.println("repetitive of 3");
           }

           else if(i%5 == 0)
           {
           System.out.println("repetitive of 5");
           }

           else if(i%5 == 0 && i%3 == 0)
           {
           System.out.println("repetitive of 3 and 5");
           }
           else{
               System.out.println(i);
           }
       }
   }
}

First of please format your code properly as it helps to read it. I’ve formatted the code as it is suggested for java:

public static void main(String args[]) {
    for (int i = 1; i < 101; i++) {
        if (i%3 == 0) {
            System.out.println("repetitive of 3");
        } else if (i%5 == 0) {
            System.out.println("repetitive of 5");
        } else if (i%5 == 0 && i%3 == 0) {
            System.out.println("repetitive of 3 and 5");
        } else {
            System.out.println(i);
        }
    }
}

Now for the review:

The third if will never be reached as the first if already matches.

So to get the result wanted you have to change the first and third if branch:

public static void main(String args[]) {
    for (int i = 1; i < 101; i++) {
        if (i%5 == 0 && i%3 == 0) {
            System.out.println("repetitive of 3 and 5");
        } else if (i%5 == 0) {
            System.out.println("repetitive of 5");
        } else if (i%3 == 0) {
            System.out.println("repetitive of 3");
        } else {
            System.out.println(i);
        }
    }
}

Using the method main() for small tests is fine but it is better to create objects and use them from the beginning.

Else the program look good so far.

A couple of quick things.

  1. Be consistent with your white space. There’s a good usage here:

     for(int i = 1; i < 101; i++)
    

    But not so much here:

     if(i%3 == 0)
    
  2. Let’s back up to your loop declaration. I think it makes more sense to say less than or equal to 100 instead if less than 101.

    for(int i = 1; i <= 100; i++)
    
  3. Don’t program in Main. Put your logic in a method and call it from Main.

Your indentation could use some work. I see you use Eclipse; This Stack Overflow answer tells you how to auto-format:

Ctrl + Shift + F

Or, in the main menu > Source > Format

It will take care of most spacing and indentation issues. You can even configure your style the way you want it (what you have now is not the standard Java dictates, see @Uwe Plonus’s answer for an example).

The result:

public static void main(String args[]){

    for (int i = 1; i < 101; i++)
    {
        if (i % 3 == 0)
        {
            System.out.println("repetitive of 3");
        }

        else if (i % 5 == 0)
        {
            System.out.println("repetitive of 5");
        }

        else
        {
            System.out.println(i);
        }

    }

}

And if you look carefully, you’ll see I snipped off a bracket. You had one too many.

In Java and other similar languages inspired by C, there are two idiomatic ways to write a for-loop that repeats 100 times.

One way is

for (int i = 1; i <= 100; i++) {
    …
}

Use that when you want i to count from 1 to 100 (inclusive).

The other way is

for (int i = 0; i < 100; i++) {
    …
}

Use that when you want i to count from 0 to 99 (inclusive). You should also prefer this form if you only care about the total number of loops, and don’t care about the values of i. (It’s more “natural” since array indices are zero-based.)

Of those two forms, for this problem, you want the first.

However, don’t mix metaphors, or you’ll be likely to shoot yourself in the foot with an off-by-one error. The number 101 doesn’t appear in the task that you are trying to solve, so it shouldn’t appear in your code either.

Leave a Reply

Your email address will not be published. Required fields are marked *