Formatted Numbers

Toph

Formatted Numbers

1200 630 Contest Hack

Read an integer variable and print it in which the digits are separated into groups of three by commas.

Input

The input will contain an integer $A$ ($0 \le A < 200000000$).

Output

Print the formatted number.

Sample

InputOutput
11711231,171,123

Solution

Python

import numpy as np

a=input()

n=len(a)
x=[0]*n
a=int(a)
for i in range(0,n):
    f=a//1000
    r=a%1000
    
    if r!=0:
        x[i]=r
        a=f
x=np.trim_zeros(x)
x.reverse()
p=",".join(str(i) for i in x)
print (p)

C++

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    while(cin>>s)
    {
        int len=s.length();
        for(int i=0; i<len; i++)
        {
            if((len-i)%3==0 && i>0)
                cout<<",";
            cout<<s[i];
        }
        cout<<endl;
    }
    return 0 ;
}

C

#include<stdio.h>
#include<string.h>

int main()
{
    char s[20];
    int i;
    gets(s);
    int k=0,j=0;
    int new[30];
    for(i=strlen(s)-1;i>=0;i--)
    {
        if(j==3)
        {
            new[k++]=',';
            new[k++]=s[i];
            j=0;
        }
        else
            new[k++]=s[i];
     j++;

    }

    for(i=k-1;i>=0;i--)
    {
        printf("%c",new[i]);
    }
}