Dividing inside a for loop. Lolwtf?

• July 23rd, 2013, 07:28 PM
samy109
Dividing inside a for loop. Lolwtf?
I have a for loop which looks like so: for(int x = 0; x<=1000; x++){ System.out.println(x/3); }

Now for some insanely strange reason, when I run the code, it repeats the awnsers however many times im dividing by. So lets say its 3 it prints 000 111 222 and so on, 4 is like 1111 2222 3333 and etc. How in the world do I fix this, i've tried a bunch of different ideas, nothing.
• July 23rd, 2013, 08:23 PM
helloworld922
Re: Dividing inside a for loop. Lolwtf?
You're using integer division, which truncates the results to an integer. You want to promote the expression type to floating point.

Code java:

```System.out.println(x / 3.); // note the . after the 3 System.out.println((double) x / 3); // same result, explicitly cast x to double and 3 gets implicitly promoted```
• July 23rd, 2013, 08:39 PM
AlexHail
Re: Dividing inside a for loop. Lolwtf?
Code :

```for(int x = 0; x <= 1000; x++) { System.out.println(x/3); }```

Sorry your code is much easier to read this way. Helloworld hit the nail on the head.

But in more detailed wording, because you are dividing x (an integer) by 3 (an integer also), your answer is going to be... you guessed it, and integer. To truncate a value is to basically shorten a number by dropping a digit(s).

In your code, when you print out x / 3, it's going to print the truncated integer from the expression x / 3 because the type Int can't hold floating point values.

So as helloworld said you need to cast your output to a float point, and you can do that by adding a period after 3, or casting the entire expression to a double.
• July 23rd, 2013, 08:39 PM
samy109
Re: Dividing inside a for loop. Lolwtf?
Quote:

Originally Posted by helloworld922
You're using integer division, which truncates the results to an integer. You want to promote the expression type to floating point.

Code java:

```System.out.println(x / 3.); // note the . after the 3 System.out.println((double) x / 3); // same result, explicitly cast x to double and 3 gets implicitly promoted```

Thanks a bunch. Now my next question, i'm asking this because i'm doing the project euler first problem, and I am COMPLETLY mind boggled at the awnser. It asks what is the sum of the multiples of 3 or 5, under 1000.

so thats 3+6+9 until the multiple is right under 1000 (995 and 999) I added all these up, and the awnser doesn't equal the awnser on project euler. What am I doing wrong? It makes me feel like they are the ones who are incorrect.

This is my code:

Code :

```package sumsof3sand5s;   public class app { public static int a; public static int x; public static int b; public static int c;   public static void main(String[] args) throws InterruptedException {   for(x = 0;x<1000;x++){ if(x%3==0){ a=a+x; System.out.println(x); } }   Thread.sleep(2000); System.out.println(""); for(int z = 0;z<1000;z++){ if(z%5==0){ b=b+z; System.out.println(z); } }     System.out.println(a+b);     }```
• July 23rd, 2013, 08:41 PM
AlexHail
Re: Dividing inside a for loop. Lolwtf?
Quote:

Originally Posted by samy109
so thats 3+6+9 until the multiple is right under 1000 (995 and 999) I added all these up, and the awnser doesn't equal the awnser on project euler. What am I doing wrong? It makes me feel like they are the ones who are incorrect.

Post your progress and I will be able to assist you, but we must locate the problem.
• July 23rd, 2013, 08:57 PM
samy109
Re: Dividing inside a for loop. Lolwtf?
Quote:

Originally Posted by AlexHail
Post your progress and I will be able to assist you, but we must locate the problem.

This is my code:

Code :

```package sumsof3sand5s;   public class app { public static int a; public static int x; public static int b; public static int c;   public static void main(String[] args) throws InterruptedException {   for(x = 0;x<1000;x++){ if(x%3==0){ a=a+x; System.out.println(x); } }   Thread.sleep(2000); System.out.println(""); for(int z = 0;z<1000;z++){ if(z%5==0){ b=b+z; System.out.println(z); } }     System.out.println(a+b);     }```

What it does, is first it counts up the numbers 3, 6 - till 999, then gets the sum of all those numbers (166833). then it does the same for 5. 5-10, all the way to 995 (99500). Then it adds these two values up (266333). I do not see how this could be incorrect, as it does exactly what's asked.

--- Update ---

Nevermind I figured it out.
• July 23rd, 2013, 09:04 PM
AlexHail
Re: Dividing inside a for loop. Lolwtf?
You must compensate for those numbers that are counted twice.

Ex. 30 is a multiple of 3, and 5. So when determining if the iteration is a multiple of either 3 or 5, it will add that number to the sum twice.