Hi Satyam, not sure if the functionality will suit your needs but there is a way to slightly optimize it.
What is happening with your code is that while loop is getting executed 105 times and for each iteration the "if" condition is being evaluated. Unless you have logic to execute in between the frequency, the following code will reduce the iterations. The difference may be more prominent for large iterations.
I doubt it. What Satyam probably omitted was some code that is executed for each iteration. A nested loop may help though:
You'll now get a line printed for 10, 20, 30, ..., 90, 100 and finally 105.
Don't forget to add that j < runs or you will have 5 iterations too many. I've turned the 105 into a final int to make sure that i and j have the same upper bound.
Satyam, I honestly would keep your current code with one modification: it should be messageCount % frequency == 0, or you will get lines printed at 11, 21, etc and not at 10, 20, etc. If I do something like this my guard is similar:
After all, messageCount % frequency is 0 if messageCount is 0 (a case where you don't want to have a line printed) or a non-0 multitude of frequency (a case where you do want to have a line printed).