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 ?

4th May 2022, 2:05 AM
Ahmed Mahmood Mohammed
Ahmed Mahmood Mohammed - avatar
5 Answers
+ 1
What have you tried so far? Try yourself first and post your try if not solved...
4th May 2022, 11:48 AM
Jayakrishna šŸ‡®šŸ‡³
+ 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.
4th May 2022, 11:55 AM
Jayakrishna šŸ‡®šŸ‡³
+ 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.
4th May 2022, 12:15 PM
Lisa
Lisa - avatar
+ 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
4th May 2022, 11:50 AM
Ahmed Mahmood Mohammed
Ahmed Mahmood Mohammed - avatar
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)+" "); } }
5th May 2022, 12:09 PM
Thineshan Panchalingam
Thineshan Panchalingam - avatar