Print numbers between 1 to 100 which are divisble by 3 on java

Joined
Oct 4, 2022
Messages
1
Reaction score
0
Write a Java program to print numbers between 1 to 100 which are divisible by 3 while using a "while loop" and a "do-while loop"
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
Something like that?
Code:
 for(var i=1; i<101; i++){
            if((i%3)<1){
                System.out.println(i);
           }
        }

Modulo is your friend here. Google for it


If you wonder why <1 instead of ==0

Just performance. Instead of checking each result for it's exact value, we just check if the value is smaller then 1. There is no negative values in here, so that's the better way
 
Joined
Mar 28, 2022
Messages
82
Reaction score
11
Java:
    int currentValue = 3;
    do{
      System.out.println(currentValue);
      currentValue += 3;
    }
    while(currentValue <= 100);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top