top button
Flag Notify
Site Registration

Reversing a number (integer) in C?

+2 votes
228 views

input: 2761263
output: 36,216,72 (inclusion of , in the reverse number)

posted Oct 14, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

int main()
{
int in = 2761263;
int out = 0;
while(in){
out = 10*out + in%10;
in /=10;
}
printf("output %d\n",out);
}
just it will reverse the number..

answer Oct 15, 2014 by Bheemappa G
I think its half answer only he is asking "," comma insertion also in the output number :).
0 votes

Check the following code should help (however it is only for the US based numbering (you can modify the code for the other system i.e. change the position of the comma (,)

int numdigits(int number)
{
 int digits = 0;
 while (number != 0) { number /= 10; digits++; }
 return digits;
}

reverse(int x)
{
  if (x<10)
  {
    printf("%d", x);
    return;
  }

  reverse(x%10);

  if (numdigits(x)%3 == 1)
    printf(",");

  reverse(x/10);
}

main()
{
  int i=1234567;
  reverse(i);
}
answer Oct 15, 2014 by Salil Agrawal
...