It’s extremely simple and efficient because language has everything it needs to do this mathematically. It’s not a matter of programming, but solving a mathematical problem.
If you want to always show numbers with ending 5 you agree that you should start with 5?. And if you want to keep the distance between them always equal and keep the end 5 then we have a dozen difference, so we should increase the number by 10 by 10, then that’s it:
public class NumerosQueComeçamComAlgarimo5 {
public static void main(String [] args) {
for (int i = 5; i <= 200; i+= 10) System.out.println(i);
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Please do not use any solution that is not pure mathematics, and simple, it makes no sense to do otherwise more complicated.
I kept the class title, but it seems wrong, or the problem statement is wrong. If the problem is what the title says, then the algorithm is a little more complex, but you can do it mathematically yet.
For some reason the question attracted diversity of solutions. This is usually good, except when each one is more convoluted than another. There was a blackout who worked with strings that it was very complicated, others exist with a complicated logic that added nothing, only worsened. There is an answer that gives an advantage that the question does not ask, but could make sense to other people. It may be that the person does not have the fixed parameters. The problem with the answer is that it has become too complicated and incredibly abstract of least. Clearly the answer tried to abstract certain parts, but failed to abstract what was most important. The answer would be good if it did:
public class NumerosQueComeçamComAlgarimo5 {
public static void main(String [] args) {
IncrementInterval(5, 200);
}
public static void IncrementInterval(int inicio, int limite) {
for (int i = inicio; i <= limite; i+= 10) System.out.println(i);
}
}
If you want you can do a validation and ensure that the limit is greater than the range, or check that the end is between 0 and 9.
It took only setting parameters at the beginning and end instead of creating one if
meaningless and inefficient and create functions to hide something without any gain. OOP is destroying people’s heads.
I’m going to take this opportunity to let off steam about the state of our industry. People have lost track of the simple. Now everything has to be complicated to be "right". Have to do TDD, DDD, xDD, microservices, have to put in the cloud, on container, must have a build complex, having N layers, even the use of OOP is absurdly overvalued and abused. And abstraction also ends up creating excessive complexity, although often the lack of it is the real problem. It’s so hard to do a simple function and use a very simple loop?
It would not be enough to start your counter with 5 and increase by 10?
int i = 5; i <= 200; i += 10
– user140390
A clear case of "first solve the problem, then write the code". How do you intend to check if the last digit is 5?
– Woss
@Andersoncarloswoss I wanted to understand the "just", because in "they have only the number 5 as the last digit" suggests that it may have more than one last digit?
– Bacco