Skip to content
Related Articles

Related Articles

Convert the number from International system to Indian system
  • Last Updated : 05 Nov, 2020

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:

NumberApplying separatorsIn words
11One
1010Ten
100100One hundred
10001, 000One thousand
1000010, 000Ten thousand
100000100, 000One hundred thousand
10000001, 000, 000One million
1000000010, 000, 000Ten million
100000000100, 000, 000One hundred million
10000000001, 000, 000, 000One billion

Indian Numeric System: The Indian numbering system is used in the Indian subcontinent to express large numbers. It follows the following schema:

NumberApplying separatorsIn words
11One
1010Ten
100100One hundred
10001, 000One thousand
1000010, 000Ten thousand
1000001, 00, 000One lakh
100000010, 00, 000Ten lakh
100000001, 00, 00, 000One crore
10000000010, 00, 00, 000Ten crore
1000000000100, 00, 00, 000One hundred crore

Approach: From the above representations, the idea is to first obtain the number without any separators. Therefore: 



  1. Remove all the separators(, ) from the string.
  2. Reverse the string.
  3. Process the string and put a separator(, ) after the third number.
  4. Now put a separator(, ) after every second number. This converts the number into the Indian Numeric System.
  5. Reverse the string again and print it.

Below is the implementation of the above approach:
 

C++




// C++ program to convert the number
// from International system
// to Indian system
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert a number represented
// in International numeric system to
// Indian numeric system.
string convert(string input)
{
    // Find the length of the
    // input string
    int len = input.length();
 
    // Removing all the separators(, )
    // from the input string
    for (int i = 0; i < len;) {
 
        if (input[i] == ', ') {
            input.erase(input.begin() + i);
            len--;
            i--;
        }
        else if (input[i] == ' ') {
            input.erase(input.begin() + i);
            len--;
            i--;
        }
        else {
            i++;
        }
    }
 
    // Reverse the input string
    reverse(input.begin(), input.end());
 
    // Declaring the output string
    string output;
 
    // Process the input string
    for (int i = 0; i < len; i++) {
        // Add a separator(, ) after the
        // third number
        if (i == 2) {
            output += input[i];
            output += ", ";
        }
 
        // Then add a separator(, ) after
        // every second number
        else if (i > 2 && i % 2 == 0
                 && i + 1 < len) {
            output += input[i];
            output += ", ";
        }
        else {
            output += input[i];
        }
    }
 
    // Reverse the output string
    reverse(output.begin(), output.end());
 
    // Return the output string back
    // to the main function
    return output;
}
 
// Driver code
int main()
{
    string input1 = "123, 456, 789";
    string input2 = "90, 050, 000, 000";
 
    cout << convert(input1) << endl;
    cout << convert(input2);
}

Java




// Java program to convert the number
// from International system
// to Indian system
import java.util.*;
class GFG{
 
// Function to convert a number represented
// in International numeric system to
// Indian numeric system.
static String convert(String input)
{
    StringBuilder sbInput = new StringBuilder(input);
 
    // Find the length of the
    // sbInput
    int len = sbInput.length();
 
    // Removing all the separators(, )
    // from the sbInput
    for (int i = 0; i < len;)
    {
        if (sbInput.charAt(i) == ',')
        {
            sbInput.deleteCharAt(i);
            len--;
            i--;
        }
        else if (sbInput.charAt(i) == ' ')
        {
            sbInput.deleteCharAt(i);
            len--;
            i--;
        }
        else
        {
            i++;
        }
    }
 
    // Reverse the sbInput
    StringBuilder sbInputReverse = sbInput.reverse();
 
    // Declaring the output
    StringBuilder output = new StringBuilder();
 
    // Process the sbInput
    for (int i = 0; i < len; i++)
    {
 
        // Add a separator(, ) after the
        // third number
        if (i == 2)
        {
            output.append(sbInputReverse.charAt(i));
            output.append(" ,");
        }
 
        // Then add a separator(, ) after
        // every second number
        else if (i > 2 && i % 2 == 0 && i + 1 < len)
        {
            output.append(sbInputReverse.charAt(i));
            output.append(" ,");
        }
        else
        {
            output.append(sbInputReverse.charAt(i));
        }
    }
 
    // Reverse the output
    StringBuilder reverseOutput = output.reverse();
 
    // Return the output string back
    // to the main function
    return reverseOutput.toString();
}
 
// Driver code
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));
}
}
 
// This code is contributed by offbeat

Python3




# Python3 program to convert
# the number from International
# system to Indian system
 
# Function to convert a number
# represented in International
# numeric system to Indian numeric
# system.
def convert(input):
 
    # Find the length of the
    # input string
    Len = len(input)
 
    # Removing all the separators(, )
    # from the input string
    i = 0
    while(i < Len):
        if(input[i] == ","):
            input = input[:i] + input[i + 1:]
            Len -= 1
            i -= 1
        elif(input[i] == " "):
            input=input[:i] + input[i + 1:]
            Len -= 1
            i -= 1
        else:
            i += 1
    # Reverse the input string
    input=input[::-1]
 
    # Declaring the output string
    output = ""
 
    # Process the input string
    for i in range(Len):
 
        # Add a separator(, ) after the
        # third number
        if(i == 2):
            output += input[i]
            output += " ,"
         
        # Then add a separator(, ) after
        # every second number
        elif(i > 2 and i % 2 == 0 and
             i + 1 < Len):
            output += input[i]
            output += " ,"
        else:
            output += input[i]
     
    # Reverse the output string
    output=output[::-1]
 
    # Return the output string back
    # to the main function
    return output
 
# Driver code
input1 = "123, 456, 789"
input2 = "90, 050, 000, 000"
print(convert(input1))
print(convert(input2))
 
# This code is contributed by avanitrachhadiya2155
Output: 
12, 34, 56, 789
90, 05, 00, 00, 000

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :