Given a number N. Convert N into an Indian currency format. Rules of Indian Currency Format are:
- The last three digits (rightmost) remain grouped together.
- Beyond the last three digits, groups of two digits are separated by commas.
Examples:
Input: N = 1000000
Output: Rs 10, 00, 000
Explanation: Start from the right: 000 (last three digits remain as is).
The remaining digits are grouped in twos: 10,00
Final format: 10,00,000Input: N = 1500
Output: Rs 1,500
Explanation: Last three digits remain as is: 500
The remaining digit forms a single group: 1,500
Final format: 1,500
Approach:
Steps involved in the implementation of code:
- We need to check whether the length of the string is even or odd.
- If the length of the string is less than equal to 3 we will simply return it else we will do the following, newString = "".
- If the length is even:
- we will add the first character into a new string newSstring = N[0].
- and then we will add comma to the new string = ", ".
- Now we will skip the two characters and then add ", " till the length < (n-2).
- And at last, we will add the remaining characters to the newString.
// C++ implementation of the code
#include <bits/stdc++.h>
using namespace std;
// Function to convert N
// into indian currency
string goodFormat(string s, int n)
{
// If length if less than 3
if (n <= 3)
return "Rs. " + s;
string ans = "";
int start = 0, cnt = 0;
// If length is even
if (n % 2 == 0) {
ans += s[0];
ans += ", ";
start = 1;
}
while (start < n - 2) {
if (cnt == 2) {
ans += ", ";
cnt = 0;
continue;
}
else {
ans += s[start];
cnt++;
}
start++;
}
for (int i = start; i < n; i++)
ans += s[i];
return "Rs " + ans;
}
// Drivers code
int main()
{
string s = "1000000";
int l = s.length();
// Function Call
cout << goodFormat(s, l);
return 0;
}
// Java implementation of the code
import java.util.*;
class GFG {
// Function to convert N
// into Indian currency
static String goodFormat(String s, int n)
{
// If length is less than 3
if (n <= 3)
return "Rs. " + s;
String ans = "";
int start = 0, cnt = 0;
// If length is even
if (n % 2 == 0) {
ans += s.charAt(0);
ans += ", ";
start = 1;
}
while (start < n - 2) {
if (cnt == 2) {
ans += ", ";
cnt = 0;
continue;
}
else {
ans += s.charAt(start);
cnt++;
}
start++;
}
for (int i = start; i < n; i++)
ans += s.charAt(i);
return "Rs " + ans;
}
// Drivers code
public static void main(String[] args)
{
String s = "1000000";
int l = s.length();
// Function Call
System.out.println(goodFormat(s, l));
}
}
// This code is contributed by prasad264
# Python3 implementation of the code
# Function to convert N
# into indian currency
def goodFormat(s, n):
# If length if less than 3
if (n <= 3):
return "Rs. " + s
ans = ""
start = 0
cnt = 0
# If length is even
if (n % 2 == 0):
ans += s[0]
ans += ", "
start = 1
while (start < n - 2):
if (cnt == 2):
ans += ", "
cnt = 0
continue
else:
ans += s[start]
cnt += 1
start += 1
for i in range(start, n):
ans += s[i]
return "Rs " + ans
# Drivers code
s = "1000000"
l = len(s)
# Function Call
print(goodFormat(s, l))
using System;
public class IndianCurrencyFormatter
{
public static string GoodFormat(string s)
{
int n = s.Length;
// If length if less than 3
if (n <= 3)
return "Rs. " + s;
string ans = "";
int start = 0, cnt = 0;
// If length is even
if (n % 2 == 0)
{
ans += s[0];
ans += ", ";
start = 1;
}
while (start < n - 2)
{
if (cnt == 2)
{
ans += ", ";
cnt = 0;
continue;
}
else
{
ans += s[start];
cnt++;
}
start++;
}
for (int i = start; i < n; i++)
ans += s[i];
return "Rs " + ans;
}
public static void Main()
{
string s = "1000000";
// Function Call
Console.WriteLine(GoodFormat(s));
}
}
// Javascript implementation of the code
// Function to convert N
// into indian currency
function goodFormat(s, n)
{
// If length if less than 3
if (n <= 3)
return "Rs. " + s;
let ans = "";
let start = 0, cnt = 0;
// If length is even
if (n % 2 == 0) {
ans += s[0];
ans += ", ";
start = 1;
}
while (start < n - 2) {
if (cnt == 2) {
ans += ", ";
cnt = 0;
continue;
}
else {
ans += s[start];
cnt++;
}
start++;
}
for (let i = start; i < n; i++)
ans += s[i];
return "Rs " + ans;
}
// Drivers code
let s = "1000000";
let l = s.length;
// Function Call
console.log(goodFormat(s, l));
Output
Rs 10, 00, 000
Time Complexity: O(n), as we iterate through the string once, processing each character.
Auxiliary Space: O(n), as we store the formatted output in a new string.