Open In App

Convert the number from Indian system to International system

Improve
Improve
Like Article
Like
Save
Share
Report

Given an input string

N

consisting of numerals and separators (, ) in the Indian Numeric System, the task is to print the string after placing separators(, ) based on International Numeric System.

Examples:

Input: N = “12, 34, 56, 789” Output: 123, 456, 789 Input: N = “90, 05, 00, 00, 000” Output: 90, 050, 000, 000

Approach:

  1. Remove all the separators (, ) from the string.
  2. Iterate from the end of the string and place a separator(, ) after every third number.
  3. Print the result.

Below is the implementation of the above approach:

C++




// C++ Program to convert
// the number from Indian system
// to International system
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert Indian Numeric
// System to International Numeric System
string convert(string input)
{
    // Length of the input string
    int len = input.length();
 
    // Removing all the separators(, )
    // From the input string
    for (int i = 0; i < len; i++) {
        if (input[i] == ',') {
            input.erase(input.begin() + i);
            len--;
            i--;
        }
    }
 
    // Initialize output string
    string output = "";
    int ctr = 0;
 
    // Process the input string
    for (int i = len - 1; i >= 0; i--) {
 
        ctr++;
        output = input[i] + output;
 
        // Add a separator(, ) after
        // every third digit
        if (ctr % 3 == 0 && ctr < len) {
            output = ',' + output;
        }
    }
 
    // Return the output string back
    // to the main function
    return output;
}
 
// Driver Code
int main()
{
    string input1 = "12,34,56,789";
    string input2 = "90,05,00,00,000";
 
    cout << convert(input1) << endl;
    cout << convert(input2) << endl;
}


Java




public class IndianToInternationalConversion {
    // Function to convert Indian Numeric
    // System to International Numeric System
    static String convert(String input) {
        // Remove all the separators (, ) from the input string
        input = input.replaceAll(",", "");
 
        // Initialize the output string and counter
        StringBuilder output = new StringBuilder();
        int ctr = 0;
 
        // Process the input string from right to left
        for (int i = input.length() - 1; i >= 0; i--) {
            ctr++;
            output.insert(0, input.charAt(i)); // Add the current digit to the output
 
            // Add a separator (, ) after every third digit (from right to left)
            if (ctr % 3 == 0 && ctr < input.length()) {
                output.insert(0, ',');
            }
        }
 
        // Return the output string
        return output.toString();
    }
 
    // Driver Code
    public static void main(String[] args) {
        String input1 = "12,34,56,789";
        String input2 = "90,05,00,00,000";
 
        System.out.println(convert(input1));
        System.out.println(convert(input2));
    }
}


Python




# Function to convert Indian Numeric
# System to International Numeric System
def convert(input_str):
    # Length of the input string
    len_input = len(input_str)
 
    # Removing all the separators (, )
    # From the input string
    i = 0
    while i < len_input:
        if input_str[i] == ',':
            input_str = input_str[:i] + input_str[i+1:]
            len_input -= 1
        else:
            i += 1
 
    # Initialize output string
    output = ""
    ctr = 0
 
    # Process the input string
    for i in range(len_input - 1, -1, -1):
        ctr += 1
        output = input_str[i] + output
 
        # Add a separator (, ) after
        # every third digit
        if ctr % 3 == 0 and ctr < len_input:
            output = ',' + output
 
    # Return the output string back
    # to the main function
    return output
 
# Driver Code
if __name__ == "__main__":
    input1 = "12,34,56,789"
    input2 = "90,05,00,00,000"
 
    print(convert(input1))
    print(convert(input2))


C#




using System;
using System.Text;
 
 class GFG
{
    // Function to convert Indian Numeric
    // System to International Numeric System
    static string Convert(string input)
    {
        // Remove all the separators (, ) from the input string
        input = input.Replace(",", "");
 
        // Initialize the output string and counter
        StringBuilder output = new StringBuilder();
        int ctr = 0;
 
        // Process the input string from right to left
        for (int i = input.Length - 1; i >= 0; i--)
        {
            ctr++;
            output.Insert(0, input[i]); // Add the current digit to the output
 
            // Add a separator (, ) after every third digit (from right to left)
            if (ctr % 3 == 0 && ctr < input.Length)
            {
                output.Insert(0, ',');
            }
        }
 
        // Return the output string
        return output.ToString();
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        string input1 = "12,34,56,789";
        string input2 = "90,05,00,00,000";
 
        Console.WriteLine(Convert(input1));
        Console.WriteLine(Convert(input2));
    }
}
 
// by phasing17


Javascript




// Function to convert Indian Numeric
// System to International Numeric System
function convert(input) {
    // Removing all the separators(, )
    // From the input string
    input = input.replace(/,/g, '');
 
    // Initialize output string
    let output = '';
    let ctr = 0;
 
    // Process the input string
    for (let i = input.length - 1; i >= 0; i--) {
        ctr++;
        output = input.charAt(i) + output;
 
        // Add a separator(, ) after
        // every third digit
        if (ctr % 3 === 0 && ctr < input.length) {
            output = ',' + output;
        }
    }
 
    // Return the output string back
    // to the caller
    return output;
}
 
// Driver Code
const input1 = '12,34,56,789';
const input2 = '90,05,00,00,000';
 
console.log(convert(input1));
console.log(convert(input2));


Output

123,456,789
90,050,000,000





Related article:

Convert the number from International system to Indian system



Last Updated : 07 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads