Convert the number from International system to Indian system
Last Updated :
20 Dec, 2023
Given string str which represents a number with separators(,) in the International number system, the task is to convert this string representation into the Indian Numeric System.
Examples:
Input: str = “123,456,789”
Output: 12,34,56,789
Explanation:
The given string represents a number in the international system. It is converted to the Indian system.
Input: str = “90,050,000,000”
Output: 90,05,00,00,000
International Numeric System: The International numbering system is used outside the Indian subcontinent to express large numbers. It follows the following schema:
1 |
1 |
One |
10 |
10 |
Ten |
100 |
100 |
One hundred |
1000 |
1,000 |
One thousand |
10000 |
10,000 |
Ten thousand |
100000 |
100,000 |
One hundred thousand |
1000000 |
1,000,000 |
One million |
10000000 |
10,000,000 |
Ten million |
100000000 |
100,000,000 |
One hundred million |
1000000000 |
1,000,000,000 |
One billion |
Indian Numeric System: The Indian numbering system is used in the Indian subcontinent to express large numbers. It follows the following schema:
1 |
1 |
One |
10 |
10 |
Ten |
100 |
100 |
One hundred |
1000 |
1,000 |
One thousand |
10000 |
10,000 |
Ten thousand |
100000 |
1,00,000 |
One lakh |
1000000 |
10,00,000 |
Ten lakh |
10000000 |
1,00,00,000 |
One crore |
100000000 |
10,00,00,000 |
Ten crore |
1000000000 |
100,00,00,000 |
One hundred crore |
Approach: From the above representations, the idea is to first obtain the number without any separators. Therefore:
- Remove all the separators(, ) from the string.
- Process the string.
- Now put a separator(, ) after the third number and then after every second number.
- Reverse the string (This converts the number into the Indian Numeric System) and print it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string convert(string input)
{
int len = input.length();
for ( int i = 0; i < len;) {
if (input[i] == ',' ) {
input.erase(input.begin() + i);
len--;
i--;
}
else {
i++;
}
}
reverse(input.begin(), input.end());
string output;
for ( int i = 0; i < len; i++) {
if (i == 2) {
output += input[i];
output += "," ;
}
else if (i > 2 && i % 2 == 0
&& i + 1 < len) {
output += input[i];
output += "," ;
}
else {
output += input[i];
}
}
reverse(output.begin(), output.end());
return output;
}
int main()
{
string input1 = "123,456,789" ;
string input2 = "90,050,000,000" ;
cout << convert(input1) << endl;
cout << convert(input2);
}
|
Java
import java.util.*;
class GFG{
static String convert(String input)
{
StringBuilder sbInput = new StringBuilder(input);
int len = sbInput.length();
for ( int i = 0 ; i < len;)
{
if (sbInput.charAt(i) == ',' )
{
sbInput.deleteCharAt(i);
len--;
i--;
}
else
{
i++;
}
}
StringBuilder sbInputReverse = sbInput.reverse();
StringBuilder output = new StringBuilder();
for ( int i = 0 ; i < len; i++)
{
if (i == 2 )
{
output.append(sbInputReverse.charAt(i));
output.append( "," );
}
else if (i > 2 && i % 2 == 0 && i + 1 < len)
{
output.append(sbInputReverse.charAt(i));
output.append( "," );
}
else
{
output.append(sbInputReverse.charAt(i));
}
}
StringBuilder reverseOutput = output.reverse();
return reverseOutput.toString();
}
public static void main(String[] args)
{
String input1 = "123,456,789" ;
String input2 = "90,050,000,000" ;
System.out.println(convert(input1));
System.out.println(convert(input2));
}
}
|
Python3
def convert( input ):
Len = len ( input )
i = 0
while (i < Len ):
if ( input [i] = = "," ):
input = input [:i] + input [i + 1 :]
Len - = 1
i - = 1
else :
i + = 1
input = input [:: - 1 ]
output = ""
for i in range ( Len ):
if (i = = 2 ):
output + = input [i]
output + = ","
elif (i > 2 and i % 2 = = 0 and
i + 1 < Len ):
output + = input [i]
output + = ","
else :
output + = input [i]
output = output[:: - 1 ]
return output
input1 = "123,456,789"
input2 = "90,050,000,000"
print (convert(input1))
print (convert(input2))
|
C#
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
class GFG {
static string convert( string input)
{
StringBuilder sbInput = new StringBuilder(input);
int len = sbInput.Length;
for ( int i = 0; i < len;) {
if (sbInput[i] == ',' ) {
sbInput.Remove(i, 1);
len--;
i--;
}
else {
i++;
}
}
StringBuilder sbInputReverse = new StringBuilder();
string st1 = new string (
sbInput.ToString().Reverse().ToArray());
sbInputReverse.Append(st1);
StringBuilder output = new StringBuilder();
for ( int i = 0; i < len; i++) {
if (i == 2) {
output.Append(sbInputReverse[i]);
output.Append( "," );
}
else if (i > 2 && i % 2 == 0 && i + 1 < len) {
output.Append(sbInputReverse[i]);
output.Append( "," );
}
else {
output.Append(sbInputReverse[i]);
}
}
StringBuilder reverseOutput = new StringBuilder();
st1 = new string (
output.ToString().Reverse().ToArray());
reverseOutput.Append(st1);
return reverseOutput.ToString();
}
public static void Main( string [] args)
{
string input1 = "123,456,789" ;
string input2 = "90,050,000,000" ;
Console.WriteLine(convert(input1));
Console.WriteLine(convert(input2));
}
}
|
Javascript
function convert(input) {
const str = input.replace(/[\s,]+/g, '' );
const arr = [];
let i = str.length - 1;
for (let j = str.length - 1; j > i; j--)
arr.push(str[j]);
for (let j = 0; i >= 0; i--, j++) {
if (j > 2 && j % 2 == 1) arr.push( ',' );
arr.push(str[i]);
};
return arr.reverse().join( '' );
};
const input1 = '123,456,789' ;
const input2 = '90,050,000,000' ;
const input3 = '48,605,000,000' ;
console.log(convert(input1));
console.log(convert(input2));
console.log(convert(input3));
|
Output
12,34,56,789
90,05,00,00,000
Time Complexity: O(n2), where n is the length of the string
Auxiliary Space: O(1)
Convert the number to Indian System in linear time O(N)
In above solution we had used the .erase() in C++ and .deleteCharAt() in Java, but instead of using that function we can do the same using for loop. So in this method we will remove ‘,’ and ‘ ‘ (extra space) by single for loop so our time complexity will be reduced from O(n2) to O(n).
C++
#include <bits/stdc++.h>
using namespace std;
string convert(string input)
{
int len = input.length();
string onlyDigits = "" ;
for ( int i = 0; i < len; i++) {
if (input[i] != ',' ) {
onlyDigits += input[i];
}
}
len = onlyDigits.length();
reverse(onlyDigits.begin(), onlyDigits.end());
string output = "" ;
for ( int i = 0; i < len; i++) {
if (i == 2) {
output += onlyDigits[i];
output += "," ;
}
else if (i > 2 && i % 2 == 0 && i + 1 < len) {
output += onlyDigits[i];
output += "," ;
}
else {
output += onlyDigits[i];
}
}
reverse(output.begin(), output.end());
return output;
}
int main()
{
string input1 = "123,456,789" ;
string input2 = "90,050,000,000" ;
cout << convert(input1) << endl;
cout << convert(input2);
}
|
Java
import java.util.Scanner;
public class IndianNumberConversion {
static String convert(String input) {
int len = input.length();
StringBuilder onlyDigits = new StringBuilder();
for ( int i = 0 ; i < len; i++) {
if (input.charAt(i) != ',' ) {
onlyDigits.append(input.charAt(i));
}
}
len = onlyDigits.length();
onlyDigits.reverse();
StringBuilder output = new StringBuilder();
for ( int i = 0 ; i < len; i++) {
if (i == 2 ) {
output.append(onlyDigits.charAt(i)).append( "," );
}
else if (i > 2 && i % 2 == 0 && i + 1 < len) {
output.append(onlyDigits.charAt(i)).append( "," );
} else {
output.append(onlyDigits.charAt(i));
}
}
output.reverse();
return output.toString();
}
public static void main(String[] args) {
String input1 = "123,456,789" ;
String input2 = "90,050,000,000" ;
System.out.println(convert(input1));
System.out.println(convert(input2));
}
}
|
Python3
def convert(input_str):
length = len (input_str)
only_digits = ""
for i in range (length):
if input_str[i] ! = ',' :
only_digits + = input_str[i]
length = len (only_digits)
only_digits = only_digits[:: - 1 ]
output = ""
for i in range (length):
if i = = 2 :
output + = only_digits[i] + ","
elif i > 2 and i % 2 = = 0 and i + 1 < length:
output + = only_digits[i] + ","
else :
output + = only_digits[i]
output = output[:: - 1 ]
return output
if __name__ = = "__main__" :
input1 = "123,456,789"
input2 = "90,050,000,000"
print (convert(input1))
print (convert(input2))
|
C#
using System;
class Program
{
static string Convert( string inputStr)
{
int length = inputStr.Length;
string onlyDigits = "" ;
for ( int i = 0; i < length; i++)
{
if (inputStr[i] != ',' )
{
onlyDigits += inputStr[i];
}
}
length = onlyDigits.Length;
char [] charArray = onlyDigits.ToCharArray();
Array.Reverse(charArray);
onlyDigits = new string (charArray);
string output = "" ;
for ( int i = 0; i < length; i++)
{
if (i == 2)
{
output += onlyDigits[i] + "," ;
}
else if (i > 2 && i % 2 == 0 && i + 1 < length)
{
output += onlyDigits[i] + "," ;
}
else
{
output += onlyDigits[i];
}
}
charArray = output.ToCharArray();
Array.Reverse(charArray);
output = new string (charArray);
return output;
}
static void Main( string [] args)
{
string input1 = "123,456,789" ;
string input2 = "90,050,000,000" ;
Console.WriteLine(Convert(input1));
Console.WriteLine(Convert(input2));
}
}
|
Javascript
function convert(input) {
let len = input.length;
let onlyDigits = "" ;
for (let i = 0; i < len; i++) {
if (input[i] !== ',' ) {
onlyDigits += input[i];
}
}
len = onlyDigits.length;
onlyDigits = onlyDigits.split( '' ).reverse().join( '' );
let output = "" ;
for (let i = 0; i < len; i++) {
if (i === 2) {
output += onlyDigits[i] + "," ;
}
else if (i > 2 && i % 2 === 0 && i + 1 < len) {
output += onlyDigits[i] + "," ;
} else {
output += onlyDigits[i];
}
}
output = output.split( '' ).reverse().join( '' );
return output;
}
let input1 = "123,456,789" ;
let input2 = "90,050,000,000" ;
console.log(convert(input1));
console.log(convert(input2));
|
Output
12,34,56,789
90,05,00,00,000
Time Complexity: O(n), where n is the length of string
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...