Does anyone know how i would write the code for the following instructions?

Print the numbers from 3 up to 40 that are multiples of 5

Recommended Answers

All 7 Replies

yes i do, why don't you actually give it a shot first, and then we can help you out further.

This is what I have so far

for( int i = 3; i < 41; i)

    {
    System.out.println(" Numbers are " + i);
    }

yeh, so you are 50% there. now you just need a condition (e.g. if) to see if the current number is a multiple of 5 or not.

hint: modulus

That is where I am having a hard time. I thought a for loop can just be followed by a statement. I would not know where to place that % sign.

for( int i = 3; i < 41; i++)
{
     if((i%5)== 0)
     {
          System.out.println(i);
     }
}
commented: Don't give the code, when the hint is already given +0

everything within the for loop's braces are within the for loop {}. if you exclude the braces, then only a single statement is within the for loop. since you have the braces, you can place as many statements as you like within them, and they will execute with each loop (you could actually solve this issue with a single statement, but it is a little fidgety).

read what the modulus does, and it should be pretty obvious where to put it, you should only need 1 example or so.

in psuedo code you loop would probably include something like:

if the current number is a multiple of 5
  print the current number

Thanks a lot. That was very useful information!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.