Formatted Numbers

My personal solution that works:

#include <stdio.h>

int main() {
int i,a,count=0,num[1000];
scanf(“%d”,&a);

if(0<=a && a<100)
	printf("%d",a);
else
{for(i=0;a!=0;i++)
{
	num[i]=a%1000;
	a=a/1000;
	count++;
}

printf("%d,",num[count-1]);
for(i=count-2;i>0;i--)
{if (0<=num[i] && num[i]<10)
  printf("00%d,",num[i]);
 
 else if(10<=num[i] && num[i]<100)
  printf("0%d,",num[i]);
 
 else printf("%d,",num[i]);
}
if (0<=num[0] && num[0]<10)
  printf("00%d",num[0]);
 
 else if(10<=num[0] && num[0]<100)
  printf("0%d",num[i]);
else
printf("%d",num[0]);}

return 0;

}

#include<stdio.h>
int main()
{
    int n,a1,a2,a3;
    scanf("%d",&n);
    a1=n%1000;
    n/=1000;
    a2=n%1000;
    n/=1000;
    a3=n%1000;
    n/=1000;
    printf("%d,%d,%d",a3,a2,a1);
    return 0;
}

in the fourth test , it says wrong answer …
what’s the problem here ?

@Farhan2500 try 1000 or numbers<10000 with your code…

2 Likes
import java.util.Scanner;
public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int A = in.nextInt();
		
		if(A>0 && A<200000000){
			
			int num1 = A%1000;
			A= A/1000;
			
			int num2 = A%1000;
			A= A/1000;
			
			int num3 = A%1000;
			System.out.print(num3+","+num2+","+num1);
		}
	}
}

Can anyone tell me what’s the wrong in my program?

a=int(input())
if a//(10**9)!=0:
	print(str(a//(10**9)),str((a%(10**9))//(10**6)),str((a%(10**6))//1000),str(a%(10**3)),sep=",")
elif a//(10**6)!=0:
	print(str(a//(10**6)),str((a%(10**6))//1000),str(a%(10**3)),sep=",")
elif a//(10**3)!=0:
	print(str(a//1000),str(a%1000),sep=",")
else:
	print(a)

I wrote this using python 3.7.
when I test it it’s showing “1 Sample Passed”
but when I submit it’s showing wrong answer for 4th test case
Can anyone tell me what’s wrong here?

Input will not always exceed 6 digits. It can also be much smaller like 123 or 10002. In such cases your code produces leading commas. Fix the code for these cases.

Funny. Your code has same problem as the one of @user.063047.

Where is the problem? It shows wrong for a test case,

#include<stdio.h>

int main(){
int a,b,c,n;

scanf("%d",&n);
if(n<200000000 && n>999999){
    a=n%1000;
    n=n/1000;
    b=n%1000;
    n=n/1000;
    c=n%1000;
    printf("%d,%d,%d",c,b,a);
}
else if(n<1000000 && n>999){
    a=n%1000;
    n=n/1000;
    b=n%1000;
    printf("%d,%d",b,a);
}
else if(n<1000){
    a=n%1000;
    printf("%d",a);
}
return 0;

}

Try inputs with 6 or more trailing zeroes.

1 Like

I tried to solve my problem using division,comparison and modulus. But it showed wrong answer in test case 4. I know why I got it wrong. but then with a sily idea of using char\character, I got stuck in test case 1:

#include<stdio.h>
int main()
{
char a,b,c,d,e,f,g,h,i;
scanf(“%c%c%c%c%c%c%c%c%c”,&a,&b,&c,&d,&e,&f,&g,&h,&i);
printf(“%c%c%c,%c%c%c,%c%c%c”,a,b,c,d,e,f,g,h,i);
}

I know all will laugh at it. But if one modifies it in a good form, he/she will pass this problem easily. TRY if you’ve time and help me out too.

Why am I getting wrong answer? I’m sorry but I had to enlarge this problem because I’m unable to reverse a string…and copy it to another… However please help!

#include<stdio.h>
#include<string.h>
int main()
{
char a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,str[9];
int j=0,m,n;
gets(str);
n=strlen(str);
while(j<1){
if(n==9){
a=str[0];
b=str[1];
c=str[2];
d=str[3];
e=str[4];
f=str[5];
g=str[6];
h=str[7];
i=str[8];
}
else if(n==8){
b=str[0];
c=str[1];
d=str[2];
e=str[3];
f=str[4];
g=str[5];
h=str[6];
i=str[7];
}
else if(n==7){
c=str[0];
d=str[1];
e=str[2];
f=str[3];
g=str[4];
h=str[5];
i=str[6];
}
else if(n==6){
d=str[0];
e=str[1];
f=str[2];
g=str[3];
h=str[4];
i=str[5];
}
else if(n==5){
e=str[0];
f=str[1];
g=str[2];
h=str[3];
i=str[4];
}
else if(n==4){
f=str[0];
g=str[1];
h=str[2];
i=str[3];
}
else if(n==3){
g=str[0];
h=str[1];
i=str[2];
}
else if(n==2){
h=str[0];
i=str[1];
}
else if(n==1){
i=str[0];
}
j++;
}
if(a==0){
if(b==0){
if(c==0){
if(d==0){
if(e==0){
if(f==0){
if(g==0){
if(h==0){
if(i==0){
printf(“0”);
}
else {
printf(“%c”,i);
}
}
else{
printf(“%c%c”,h,i);
}
}
else{
printf(“%c%c%c”,g,h,i);
}
}
else{
printf(“%c,%c%c%c”,f,g,h,i);
}
}
else{
printf(“%c%c,%c%c%c”,e,f,g,h,i);
}
}
else{
printf(“%c%c%c,%c%c%c”,d,e,f,g,h,i);
}
}
else{
printf(“%c,%c%c%c,%c%c%c”,c,d,e,f,g,h,i);
}
}
else{
printf(“%c%c,%c%c%c,%c%c%c”,b,c,d,e,f,g,h,i);
}
}
else{
printf(“%c%c%c,%c%c%c,%c%c%c”,a,b,c,d,e,f,g,h,i);
}
return 0;
}

What would be the outputs of
a) 000,000,000
b) 0000
c) 00003
d) 3
?

#include
#include
using namespace std;
int main()
{

string a;
cin>>a;
long long int i,l=0,k=0,s=0;
l=a.size();
long long int g=l%3;
//cout<<l;
for(i=0; i<a.size(); i++)
{
    s++;
    if(s<=g)
    {
        cout<<a[i];
        k=0;
        if(s==g)
            cout<<',';
    }


    else
    {
        k++;
        cout<<a[i];

        if(k%3==0&&(s!=l))
        {
           cout<<',';
        }
    }


}

} I just got the wrong answer in last test case but don’t know why??
pls help me!!

What am I doing wrong?

#include <stdio.h>

int main()
{
long int a;
scanf("%d",&a);
int x=a/1000000;
int y=(a/1000)%1000;
int z=a%1000;

if(y!=0 && z!=0){
printf("%d,%d,%d",x,y,z);
}

else if(y!=0 && z==0){
printf("%d,%d,000",x,y,z);
}

else if(y==0 && z!=0){
printf("%d,000,%d",x,y,z);
}

else if(y==0 && z==0){
printf("%d,000,000",x,y,z);
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
	string s;
	cin>>s;
	if(s.size()<=3){
		cout << "s";
	}else if(s.size()<=6){
		if(s.size()==6){
			cout<< s[0]<<s[1]<<s[2]<<","<<s[3]<<s[4]<<s[5];
		}else if(s.size()==5){
			cout<< s[0]<<s[1]<<","<<s[2]<<s[3]<<s[4];
		}else{
			cout<< s[0]<<","<<s[1]<<s[2]<<s[3];
		}
	}else{
			if(s.size()==9){
			cout<< s[0]<<s[1]<<s[2]<<","<<s[3]<<s[4]<<s[5]<<","<<s[6]<<s[7]<<s[8];
		}else if(s.size()==8){
			cout<< s[0]<<s[1]<<","<<s[2]<<s[3]<<s[4]<<","<<s[5]<<s[6]<<s[7];
		}else{
			cout<< s[0]<<","<<s[1]<<s[2]<<s[3]<<","<<s[4]<<s[5]<<s[6];
		}
	}
	return 0;
}

why this isn’t working? THe last test case is coming wrong

@crappy_rhythms delete those quotation mark in cout << "s"; in line 7.

#include <iostream>
#include<vector>
using namespace std;

int main() {
	int A;
	cin >> A;
	vector<char>out;
	int cnt=0;
	
	while(A){
		out.push_back(char(A%10+48));
		cnt++;
		A/=10;
		if(cnt==3 && A!=0){
			out.push_back(',');
			cnt=0;
		}
	}
	for(int i=out.size()-1;i>=0;i--){
		cout<<out[i];
	}
	cout<<endl;
	
	return 0;
}

THis code getting wrong answer

Here is my code ,done by python

a=input()
a=a[::-1]
l3=[]
j=0
for i in range(len(a)):
    l3.append(a[i])
    j=j+1
    if j==3:
        l3.append(',')
        j=0
    else:
        pass
l3=''.join(l3)
l3=l3[::-1]
if ','==l3[0]:
    l3=l3[1::]
else:
    pass
print(l3)

@hjr265 , I think 0 should be added in the test cases.

1 Like

Thanks @maddog. There is such a test already. Any particular reason why you think this wasn’t a part of the test suite?