0

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

3
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by nandomendoza
0

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

0

This is what I have so far

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

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

Edited by Reverend Jim: Fixed formatting

0

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

0

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.

-1
for( int i = 3; i < 41; i++)
{
     if((i%5)== 0)
     {
          System.out.println(i);
     }
}
Votes + Comments
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
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.