Formatted Numbers

//where is the problem

#include <iostream>

using namespace std;
long long int b[3],k=-1;
void revers(long long int a){
    if(a==0){
        return;
    }

revers(a/1000);
	k++;
	b[k]=a%1000;

}
int main() {
	long long int A;
	cin >> A;

	revers(A);
	for(int i=0;i<3;i++){

        cout<<b[i];
        if(i!=2){
            cout<<",";
        }
	}
}

@ekramhossain What if the number of digits in A is less than 7. I mean your code doesn’t give the correct answer if A < 1000000.

@hjr265 , Actually when I looked at the earliest solution, I found that it has a bug. If you input 0, it’ll output " 0, "
I don’t know how but this logic had been passed.

#include <stdio.h>
int main (){
int a,b,c,d,e;
scanf("%d",&a);
b=a/1000000;
c=a/1000;
d=c%1000;
e=a%1000;
printf("%d,%.3d,%.3d",b,d,e);
return 0;
}

whats wrong with this code

a = input()
r = ''
count = 0
for i in a[::-1]:
    r += i
    count += 1
    if count == 3 and len(a) != 3:
        count = 0
        r += ','
print(r[::-1])

Not understanding why my code is failing the test

@user.829677 Your code prints leading comma if a has 6 digits.

I hope this will help a lot…

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef deque<ll> di;
typedef pair<ll,ll> pi;
#define pb push_back
#define mp make_pair
#define mh make_heap
#define pf push_front
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define INF 1000000000
#define FOR(i,a,b) for(ll i=a;i<b;i++)
const ll mod=1e9+7;

int main()
{
    //freopen("contest.txt","r",stdin);
    //freopen("contest.txt","w",stdout);
    IOS
    string s;
    vi pos;
    cin>>s;
    for(ll i=s.size()-1;i>=0; i=i-3)
    {
        pos.pb(i);
    }
    sort(pos.begin(),pos.end()); pos.pop_back();
    sort(pos.rbegin(),pos.rend());
    for(ll i=0; i<s.size() ; i++)
    {
        if(!pos.empty()){
        if(i==pos[pos.size()-1])
        {
            cout<<s[i]<<",";
            pos.pop_back();

        }
        else
            cout<<s[i];


        }
        else
            cout<<s[i];


    }


    return 0;
}

Would you please explain me the error here ?

# include <stdio.h>
char n[10] ;

int len (char ara [])
{
    int i;
    for (i = 1; ara[i] != '\0'; i ++);
    return i;
}

int main ()
{
    scanf ("%s", &n);
    int i = len (n);
    int j = i%3;
    int k;
    for (k = 0; k < i; k ++){
        
        printf ("%c", n [k]);
        
        if (k == (j-1)){
            
            if (j == i){
                return 0;
            }
            
            printf (",");
            j++;
            j++;
            j++;
        }
        
    }
    return 0;
}

A = int(input())
num = “{:,}”.format(A)
print(num)

try this one

Why i am getting output error!!

#include <stdio.h>
 int main()
 { 
     long int n;
     int k1,k2,k3;
     scanf("%ld",&n);
     if(n<=999)
        {
         		printf("%ld\n",n);
        }
    else if(n<=999999)
        {
         		k1=n%1000;
         		k2=n/1000;
         		printf("%d,%d",k2,k1);
         	}
    else if(n<=2000000000){
         		k1=n%1000;
         		k2=n%1000000;
        		k2=k2/1000;
         		k3=n/1000000;
         		printf("%d,%d,%d",k3,k2,k1);
         	} 	 	 	
	 return 0; 
	 
	 
	 
	 }

haha! lol,
you guys are so confused!
here my code is:

[REDACTED]

that’s it!
nothing else!!

Is there any way of getting the inputs of the test cases? My code is getting wrong answer in test case 6. it would have been beneficial for me if I get the input.

my written code is (written using python)

x = int(input())
length = len(str(x))
if length % 3 == 0:
	loop = length / 3
else:
	loop = (length / 3) + 1
l = []
for i in range(int(loop)):
	q = x / 1000
	r = x % 1000
	x = int(q)
	l.append(r)
l.reverse()
str_l = [str(a) for a in l]
for i in range(len(str_l)):
	if str_l[i] == '0':
		str_l[i] = '000'
	
print("," . join(str_l))
1 Like

Make sure you have tested your code with the edge cases. See the input section, and test with the minimum possible and maximum possible values of A.

image

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

#define MAX 200000000

int main(void)
{

char s[MAX];
char x[MAX + 3];
scanf("%s", s);
int i, j, n = strlen(s), count =0;
for (i = n - 1, j = n - 1; i >= 0 || j >= 0; i--, j--)
{
	if (count == 3 || count == 6 || count == 9)
	{
		x[j] = ',';
		j--;
	}
	x[j] = s[i];
	count++;
}
printf("%s", x);

return 0;

}
// i am keep getting segmentation fault, what is the problem

I’m continuously getting wrong answer in test case 3. I updated my code in different styles for a number of times but didn’t understand what’s wrong.
Please someone help me finding the problem.
My code is here:

A = int(input())

def formatter(num, Num): #num = original number, Num = listed Number
	if len(Num)<3:
		print(num)

	else:
		for j in range(len(Num)%3):
			print(Num.pop(0), end = "")

		for m in range(len(Num)):
			if m%3 == 0:
				print(",%s" %Num[m], end = "")
			else:
				print(Num[m], end = "")


formatter(A, list(str(A)))

This is somehow not passing the tests

#include <iostream>
#include <cstdio>
void recur(char c,int k,int *num)
{
    if(c!='\n')
    {
        *num=k;
        recur(getchar(),++k,*(&num));
        if(*num==k-1)
        {
            int a;//Counting the number of commas required
            a=(int)((*num+1)/3);
            if((*num+1)%3==0)//If 6 digits than number of commas will be only 1 but if 7 then it would be 2
                a--;
            for(int i=(*num-k+1);i<(*num)+a;i++)//The loop can also work from 0 to k-1+a 
                std::cout<<"_";//_ is a temp value and can be replaced and has no particular significance
        }
        else
            std::cout<<"\b\b";//Puts the cursor on the '_' before the current cursor position
        std::cout<<c;//Prints the number
        if((*num-k-1)%3==0&&(k-1)!=0)
            std::cout<<"\b\b,";
    }
    else 
        return;
}
int main()
{
    int num=0;
    recur(getchar(),0,&num);
    return 0;
}

what is the problem in this code?

#include <bits/stdc++.h>
using namespace std;
int num(int a[],int i, int x)
{
	i=1;
	while(x!=0){
		a[i]=x%10;
		x/=10;
		i++;
	}
  return i;
}
int main()
{
	int X,arr[100],j=0;
	cin>>X;
	if(X==0)
		cout<<"0";
	int n=num(arr,j,X);
    for(int i=n-1; i>=1; i--){
        if((n-1)==3)
        	cout<<arr[i];
        else{
		if(i%3==0)
			cout<<",";
			cout<<arr[i];
		}
	}
}
A = int(input())
strint=str(A)
cat=""
dog=""
hen=""
var1=strint[0]
cat+=var1+","
var2=strint[1:4]
dog+=var2+","
var3=strint[-3::]
hen+=var3
var4=(cat+dog+hen)
print(var4)

This code is giving the desired output what they actually wanted if I run in other IDE.But they are not taking it when I am submitting.What’s wrong in this code?

@Sabiha.101735 You should test your code for various inputs before submitting. For example here I tried to test your code against some made up samples:

It is apparent that your code only works when the there is 7 digits on the input. But it is said that input can be anywhere between 0 to 200,000,000 [inclusive].

Thus your code fails.

#include <iostream>

using namespace std;

int main() {
	int A;
	cin >> A;
	string str = to_string(A);
	int n = str.length();
    if(A ==0 || A == 200000000){
        cout << str;
    }
	if (0 <= A && A < 200000000) {
	string formatted;
	int commaPos = n % 3;
	 if (commaPos == 0) commaPos = 3;
	formatted += str.substr(0, commaPos);
	while (commaPos < n) {
        formatted += ",";
        formatted += str.substr(commaPos, 3);
        commaPos += 3;
    }
	cout << formatted;
	}
	return 0;
}

What’s wrong in my code? it fails the last test case