Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Dividing inside a for loop. Lolwtf?

1. ## 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.

2. ## 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.

```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```

3. ## Re: Dividing inside a for loop. Lolwtf?

```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.

4. ## Re: Dividing inside a for loop. Lolwtf?

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.

```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:

```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);

}```

5. ## Re: Dividing inside a for loop. Lolwtf?

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.

6. ## Re: Dividing inside a for loop. Lolwtf?

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:

```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.

7. ## 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.