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

# Thread: Between 100 and 200.

1. ## Between 100 and 200.

So i wanted to write a code that, when it starts, prints out numbers between 100 and 200 that can be devided between 4 or 5 but not both!
Really hard to figure out how i should go about doing so.

Any tips?

2. ## Re: Between 100 and 200.

What have you tried?

3. ## Re: Between 100 and 200.

Nothing.. i am stuck and i don't know what to do xD

4. ## Re: Between 100 and 200.

So i wanted to write a code that, when it starts, prints out numbers between 100 and 200
Have you tried until this step ?

5. ## Re: Between 100 and 200.

i am stuck and i don't know what to do xD
Do it in steps.
First step is the class and method with a loop for the desired range of values. Inside the loop call a method to determine whether to print the current value or not.
Second step would be to write the method that decides whether the value should be printed.

6. ## Re: Between 100 and 200.

Sometimes it helps to think about how you would do it on paper. Do you know how to decide if a number is divisible by 4? By 5?

Regards,
Jim

7. ## Re: Between 100 and 200.

Hint: Modulus operator...

8. ## The Following User Says Thank You to Tokugawa For This Useful Post:

johndoe123 (November 19th, 2018)

9. ## Re: Between 100 and 200.

I agree with tokugawa modulus would be the best way

10. ## Re: Between 100 and 200.

```public static void main(String[] args) {

int number = 0;

for (int i = 200; i > 100; i--) {
i = i / 4 ^ 5;
i = number;
}

System.out.println(number);

}

}```

This is what i came up with really quick.
I am not that great with modulus. But i will try!
Btw the code doesnt even work.. it skips the for loop and just says that my sum is 0.

11. ## Re: Between 100 and 200.

says that my sum is 0.
I don't see a variable named sum.

The variable: number is given a value of 0, its value is never changed so that the print statement will print a 0.

What does the code in post#9 have to do with the assignment given in post#1?

Why does the loop go from 200 to 100? vs the other way?

12. ## Re: Between 100 and 200.

Originally Posted by Norm
I don't see a variable named sum.

The variable: number is given a value of 0, its value is never changed so that the print statement will print a 0.

What does the code in post#9 have to do with the assignment given in post#1?

Why does the loop go from 200 to 100? vs the other way?

I meant "number".
I know that it doesn't change. Trying to figure it out with modulus rn.
The assignment was that i needed numbers that go from 100 to 200 or 200 to 100 that devide by 4 or 5 but not both
and which gives me the end values. Sorry if i am making this difficult but english isn't my main language and i am trying to
solve this. Haha!
But i will take any help.

13. ## Re: Between 100 and 200.

You're not using the modulus operator. It is a percent '%' sign. It will give the remainder when applied to a number.

If a is set to 103, a % 10 will yield 3.

And what is the purpose of your loop? You are doing some processing in the loop but the print statement is outside the loop. You need to think about this, so do the following:

1. Try printing numbers from 100 to 200. This is a simple loop exercise.
2. Then try printing numbers divisible by 4.
3. Then try printing numbers divisible by 5.
4. Then try printing numbers divisible by 4 or 5 but not both.

Regards,
Jim

14. ## Re: Between 100 and 200.

Can you describe the individual steps in English that you would do manually to determine if a number is divisible by 4 or 5 but not both?
You need to work out the logic before trying to write any code.
When you have the steps to solve the problem then try writing the code for them.

15. ## Re: Between 100 and 200.

Okay so i followed both of your steps.
I can get the numbers that i am searching for!
However before i did that i took, ex, 104 / 4 = 26.
And then the highest which would be 195 / 5 = 39.
I then used those as start and end points and then just took those
times 4 or 5.

Works but i am guessing that there is a better way to do this!
Also i don't believe that every number cannot be divided by both 4 and 5
at the same time.

```	public static void main(String[] args) {

for (int i = 26; i <= 39; i++) {
int numbers = i;
int numbers1 = i;
numbers = 4 * numbers;
numbers1 = 5 * numbers1;
System.out.println(numbers);
System.out.println(numbers1);

}

}
}```

16. ## Re: Between 100 and 200.

Can you post the program's output? Have you tested the results manually?

However before i did that i took, ex, 104 / 4 = 26.
And then the highest which would be 195 / 5 = 39.
I then used those as start and end points
What is the logic for using those numbers?
Why test with such a short range of numbers for the loop? What happens when the code uses 100 and 150 for the loop control vs 26 and 39?

For example
100 is divisible by both 4 and 5
104 by 4 not 5
105 by 5 not 4

These are the numbers that you program should find.

Before trying to write any more code, make the list of steps the code should take to solve the problem and post it here so we can help you work out the logic.

17. ## Re: Between 100 and 200.

Originally Posted by Norm
Can you post the program's output? Have you tested the results manually?

What is the logic for using those numbers?
Why test with such a short range of numbers for the loop? What happens when the code uses 100 and 150 for the loop control vs 26 and 39?

For example
100 is divisible by both 4 and 5
104 by 4 not 5
105 by 5 not 4

These are the numbers that you program should find.

Before trying to write any more code, make the list of steps the code should take to solve the problem and post it here so we can help you work out the logic.

What i think it should do.
1. The program should know that it only can choose from 100 to 200.
2. It should be able to print out every value from 100 to 200 (which it can if we begin from scratch).
It isn't too hard to do.
3. It should be able to take the numbers that are divisible by 4.
4. Print those out!
5. It should be able to take the numbers that are divisible by 5.
6. Print those out.
7. An if statement saying if a value of a divisible both can be divided by 4 and 5
then delete that value.

Something along those lines i believe.

18. ## Re: Between 100 and 200.

Steps 2 and 3 and 4 and 5 and 6 don't agree with the assignment.
Only the numbers divisible by 4 or by 5, NOT BOTH, should be printed.
The steps you have posted does not limit what is printed to only those that meet those conditions.

Ok as an exercise, write a program that does the steps you listed in post#16.
Be sure to label all the printed lines so we can tell why they were printed.
For example:
100 divisible by 4
...
100 divisible by 5
105 divisible by 5
...

19. ## Re: Between 100 and 200.

The fundamental question still remains.

Do you know how to determine if a number is evenly divisible by another number?

That is essential to completing the assignment. Doing your multiplication solution won't work and is probably not in the spirit of the assignment.

Regards,
Jim

20. ## Re: Between 100 and 200.

Originally Posted by jim829
The fundamental question still remains.

Do you know how to determine if a number is evenly divisible by another number?

That is essential to completing the assignment. Doing your multiplication solution won't work and is probably not in the spirit of the assignment.

Regards,
Jim
No i do not.

And no it isn't in the spirit of the assignment :/

So i feel rather stuck atm.

21. ## Re: Between 100 and 200.

No i do not.
Write a small test program with a loop (say 0 to 20) that prints out the value of the loop index with some modulus value:
For example
`   System.out.println("i=" + i + " mod 4= " + (i % 4));`

The print out will show you what is returned by modulus.

22. ## Re: Between 100 and 200.

Originally Posted by Norm
Write a small test program with a loop (say 0 to 20) that prints out the value of the loop index with some modulus value:
For example
`   System.out.println("i=" + i + " mod 4= " + (i % 4));`

The print out will show you what is returned by modulus.

So what i get is:
```i = 0 mod 4 = 0
i = 1 mod 4 = 1
i = 2 mod 4 = 2
i = 3 mod 4 = 3
i = 4 mod 4 = 0
i = 5 mod 4 = 1
i = 6 mod 4 = 2
i = 7 mod 4 = 3
i = 8 mod 4 = 0
i = 9 mod 4 = 1
i = 10 mod 4 = 2
i = 11 mod 4 = 3
i = 12 mod 4 = 0
i = 13 mod 4 = 1
i = 14 mod 4 = 2
i = 15 mod 4 = 3
i = 16 mod 4 = 0
i = 17 mod 4 = 1
i = 18 mod 4 = 2
i = 19 mod 4 = 3
i = 20 mod 4 = 0```

```public class Test1 {
public static void main(String[] args) {

for (int i = 0; i <= 20; i++) {
System.out.println("i = " + i + " mod 4 = " + (i % 4));
}

}

}```

So what we are seeing here is that when something can be divided by 4 without having values coming out after it then it turns out to be 0.
So 4 goes in 8 two whole times but it also goes in 9 two whole times but it leaves out 1. Just using those as examples.

So i get that part. In other words... i need to make a code that does the modulus of numbers between 100 and 200 without begin able to
divide by both 4 and 5 but that also won't leave numbers behind. Because then it won't divide as we want it to.

Am i right in that assumption? Or atleast on the right track?

23. ## Re: Between 100 and 200.

when something can be divided by 4 without having values coming out after it then it turns out to be 0.
When the mod value is 0, that means the number is evenly divisible. Use that test to determine if a number is divisible.
The assignment needs to test if a number is divisible by 4 and if divisible by 5.

What is the logic for the program? Each number needs two tests using: mod 4 and mod 5

Try writing a program like jim829 suggested in post#12

24. ## Re: Between 100 and 200.

Originally Posted by Norm
When the mod value is 0, that means the number is evenly divisible. Use that test to determine if a number is divisible.
The assignment needs to test if a number is divisible by 4 and if divisible by 5.

What is the logic for the program? Each number needs two tests using: mod 4 and mod 5

Try writing a program like jim829 suggested in post#12
```public class Test1 {
public static void main(String[] args) {

for (int i = 104; i <= 196; i++) {
if ((i % 5 == 0) && (i % 4 == 0)) {
System.out.print("");
} else if ((i % 4 == 0) ^ (i % 5 == 0)) {
System.out.println(i);

//System.out.println("i = " + i + " mod 4 = " + (i % 4));
//} else if (i % 5 == 0) {
//System.out.println("i = " + i + " mod 5 = " + (i % 5));
}

}

}

}```

Got it to work! Thanks guys.