Program to format a number with thousands separator in C/C++
Given an integer N, the task is to print output of the given integer in international place value format and put commas at the appropriate place, from the right.
Input: N = 47634
Output: 47, 634
Input: N = 1000000
Output : 1, 000, 000
Approach: Follow the steps below to solve the problem:
- Convert the given integer N to its equivalent string.
- Iterate over the characters of the given string from the right to the left.
- After traversing every 3 characters, insert a ‘,’ separator.
Below is the implementation of the above approach:
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Participate in the Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students.