Given an integer n and character ch, return a String using the character as thousands separator on the given number.
Input: n=1234 , ch =’.’
In the above-given input, “.” character is used as the thousands separator and is placed between hundreds and thousands of places starting from the right. The obtained output is returned as String Format.
Input: n=123456789 , ch =’.’
- Convert number into a string.
- Start string traversal from the right side.
- Add the separator character after every three digits
Below is the code using the same approach using Java.
Time Complexity: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.