# Formatted Numbers

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

If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

``````#include<stdio.h>
int  main(){
int k1,k2,k3;
int n;

scanf("%d",&n);
if(n<20000000){
k3=n%1000;
n= n/1000;
k2=n%1000;
n=n/1000;
k1=n%1000000;
printf("%d,%d,%d",k1,k2,k3);
}
return 0;

}
``````

why this judged as wrong ans

2 Likes

@KAS.shim1998 Change this to `n < 200000000`. Plus, you can drop the `if` if you want.

Here is my code. Why am I getting wrong answer in test case 4?
#include <stdio.h>

int main(void)
{
int n, t0, t1, t2;
int ok = 0;

``````scanf("%d", &n);

if (n >= 1000000){
t0 = n%1000;
n /= 1000;
t1 = n%1000;
t2 = n/1000;
ok=1;
}
else if (n >= 10000){
t0 = n%100;
n /= 100;
t1 = n%100;
t2 = n/100;
ok=1;
}
else if(n >= 100){
t0 = n%10;
n /= 10;
t1 = n%10;
t2 = n/100;
ok=1;
}
if (ok) printf("%d,%d,%d\n", t2,t1,t0);
else {
if (n>=10){
t0 = n%10;
t1 = n/10;
printf("%d,%d,%d\n",0,t1,t2);
}
else {
printf("%d,%d,%d\n",0,0,n);
}
}

return 0;
``````

}

@md_jakariya Try some inputs that have trailing zeroes. For example `1000 ` should be printed as `1,000`

Thank you. But it would be better (I think) if A (100<=A<2000000000) was written in input description.

1 Like

@md_jakariya That is a good point. We have updated the statement and added an additional test case.

My program is keep returning wrong answer in the last test(6). What should I do? Where is my problem?
Here is My CODE(Java JDK 11.22)

import java.util.Scanner;
public class toph {
public static void main(String[] args) {

``````    Scanner s = new Scanner(System.in);
int num = s.nextInt();
String str = "";
int i = 1;
if(num ==0 || num == 200000000){
System.out.println(num);
}
if (0 <= num && num < 200000000) {
while (num != 0) {
str = num % 10 + "" + str;
num = num / 10;

if (i % 3 == 0 && num != 0) {

str = "," + str;
}

i++;
}
System.out.println(str);

}

}
``````

}

Hey guys, every time I submit this, it shows wrong answer in case 6. if anyone gives me the solution I will be pleased…C

``````#include <stdio.h>

int main()
{
int n, i1, i2, i3;

scanf("%d", &n);

if (n % 1000 == 0) {
i3 = 0;
}
else {
i3 = n % 1000;
}
n = n / 1000;

if (n % 1000 == 0) {
i2 = 0;
}
else {
i2 = n % 1000;
}
n = n / 1000;

if (n % 1000 == 0) {
i1 = 0;
}
else {
i1 = n % 1000;
}

if (i1 == 0) {
printf("000,");
}
else {
printf("%d,", i1);
}

if (i2 == 0) {
printf("000,");
}
else {
printf("%d,", i2);
}

if (i3 == 0) {
printf("000");
}
else {
printf("%d", i3);
}

return 0;
}
``````

@Farhan2500 Try your code with the edge cases. See the limits of `A`.

``````#include <string>
#include <iostream>

int main()
{
std::string strn;
std::cin >> strn;
char ch = '0';
int x = 0, count = 0, dgcount = 0;

for(char &c : strn)
{
if(std::isdigit(c))
++dgcount;
}

for(auto &c : strn)
{
if((strn.size() > 3) && (strn[0] != ch) && std::isdigit(c) && (strn.size() == dgcount))
{
if(x == 0)
{
std::cout << c << ",";
}
if(count == 3)
{
std::cout << ",";
count = 0;
}
if(x == 2)
{
std::cout << c;
++count;
}
x = 2;
}
/*else
std::cout << c;*/
}
}
``````

Why am I getting wrong answer in the 2nd case?

First of all, this part of the condition seems odd:

``````(strn[0] != ch)
``````

Since this is how you initialized `ch`:

``````char ch = '0';
``````

This condition is comparing `strn[0]` for the digit 0, not the null character.

Next, you are going from left to right to add comma. Although that is technically possible, I don’t see the math in your code required to do that. The use of `x` and `count` seems a bit convoluted.

Can you explain what your thought process was behind this code? May be I can then help you simplify that and as a result you will be able to write a simpler solution for this problem.

First of all I wrote that char ch = ‘0’ and I was not expecting a null character(/0).I know my logic is a little unclear to the readers. But what I meant is, if the first character in the input is 0 then don’t add a comma after that(since I misleadingly thaught the commas go from left to right).I added the x as 0 and changed to 2 because I wanted it to ignore that if() fafte adding the first comma

And what the count does is, after 3 character outputs, it prints a comma and resets the count to 0

If you use this input with your code:

``````123456789
``````

The output you get is

``````1,234,567,89
``````

The output that is expected is

``````123,456,789
``````

So you need to group the numbers from the right side. Each group will have three digits. The left most group can have less than 3 digits.

`````` 12,345,678
1,234,567
123,456
12,345
1,234
123
12
1
``````
2 Likes

got this. Thanks . Answer is accepted now

1 Like
``````[Redacted]
``````

My problem is solved

WoW! That’s great.
I have submitted it and it was accepted.
Can you give the source where you had found it?

``````#include <stdio.h>

int main()
{
int a,b,c,d;
scanf("%d",&a);
b=a%1000;
c=a%1000000;
d=d%1000000000;
printf("%d,%d,%d",&d,&c,&b);
return 0;
}
``````

problem ???