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.

Results 1 to 23 of 23

Thread: Between 100 and 200.

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: Between 100 and 200.

    What have you tried?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Between 100 and 200.

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

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    258
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default 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 ?
    Whatever you are, be a good one

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default 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. #7
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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. #8
    Junior Member
    Join Date
    Nov 2018
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Between 100 and 200.

    I agree with tokugawa modulus would be the best way

  10. #9
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default 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. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Between 100 and 200.

    Quote Originally Posted by Norm View Post
    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. #12
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default 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
    Last edited by jim829; November 21st, 2018 at 03:09 PM.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default 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. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Between 100 and 200.

    Quote Originally Posted by Norm View Post
    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. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default 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
    ...
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default 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. #19
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Between 100 and 200.

    Quote Originally Posted by jim829 View Post
    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. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Between 100 and 200.

    Quote Originally Posted by Norm View Post
    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. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Between 100 and 200.

    Quote Originally Posted by Norm View Post
    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.

Similar Threads

  1. while(true){ section of code runs 200 times}
    By jack_nutt in forum Java Theory & Questions
    Replies: 7
    Last Post: June 23rd, 2011, 07:06 AM
  2. Want to upload csv file of greter than 200 Mb
    By shreyansh in forum Java Servlet
    Replies: 6
    Last Post: February 8th, 2011, 06:43 PM
  3. Display prime numbers from 100 to 200 in Java
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 25th, 2011, 02:14 PM
  4. How to make this print only the first 200 results?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 03:29 AM

Tags for this Thread