0
Java 000-999
Can you write a code in java that write 000 001 002 003 ā¦ 014 ā¦573ā¦ā¦ā¦..999? Print all numbers from 000 to 999 ?
5 Answers
+ 1
What have you tried so far? Try yourself first and post your try if not solved...
+ 2
Ahmed Mahmood Mohammed Share your try
Generally 01 displayed as 1 only.
Use formating option like in c as %3d
Or use 3 loop for each digit but it's efficient.
+ 2
String formatting:
https://www.tutorialspoint.com/java-program-to-add-leading-zeros-to-a-number
Or check if the number is < 10, < 100, < 1000 and put the necessary number of 0 before the number when printing.
+ 1
Jayakrishnaš®š³ i already tried but the reasult was 0 1 2 3 ā¦11ā¦25ā¦.126ā¦.999. The zeros of 0 and 1 and 2ā¦.etc lost wonāt write
0
https://www.baeldung.com/java-number-formatting
class Test{
public static String byPaddingZeros(int value, int paddingLength) {
return String.format("%0" + paddingLength + "d", value);
}
public static void main(String[] args) {
for(int value = 0;value<1000;value++)
System.out.print(byPaddingZeros(value, 3)+" ");
}
}