We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here we used printf to repeat the values of 0 and space.
'-%15s' means that the length of string will be 15 and spaces will be repeatedly printed on right side of string.
And '%03d' means length of string will be 3 and '0' will be repeatedly printed on left side of string.
because with printf we can print strings in a specified format and specific arguments and here in this question we had to print the string with specific number of characters and in a format.
As far as I know we can print zeroes in front of a number for a specific length. Here I wrote "%03d" meaning the length of number will be three and we add zeroes to cover up the length. We can also have empty spaces on both sides by writing positive or negative number like we did in "%-15s". We can also add other characters like , or . and many more. You can check out string format to know more about it.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Java Output Formatting
You are viewing a single comment's thread. Return to all comments →
Here we used printf to repeat the values of 0 and space. '-%15s' means that the length of string will be 15 and spaces will be repeatedly printed on right side of string. And '%03d' means length of string will be 3 and '0' will be repeatedly printed on left side of string.
why are we use printf
because with printf we can print strings in a specified format and specific arguments and here in this question we had to print the string with specific number of characters and in a format.
Can you please explain this part of the code:
System.out.printf("%03d",x);
It is fine that you mentioned it has 3 digits, but how do you let the system know, that it should print the number "0" to the left?
As far as I know we can print zeroes in front of a number for a specific length. Here I wrote "%03d" meaning the length of number will be three and we add zeroes to cover up the length. We can also have empty spaces on both sides by writing positive or negative number like we did in "%-15s". We can also add other characters like , or . and many more. You can check out string format to know more about it.