reversing a long sequence in c

0

I need to invert a sequence of numbers, for example, if I insert 1234, I print 4321. The code is working, but for entries of type 0123 or 1230 the zero is simply "deleted", but I needed zero was also displayed on the screen. I could not think or find a solution to this, can anyone help me?

int main()
{
    long n;
    long inverso;

    scanf("%ld",&n);

    do
    {
        inverso=n%10;
        printf("%ld",inverso);
        n/=10;

    }while(n>0);
    printf("\n");
    return 0;
}
    
asked by anonymous 26.04.2017 / 20:13

1 answer

1

Use the 0 * specifier to specify the field size to be printed with leading zeros. For example for a 5-position field:

printf("%0*lld\n", 5, 123);

In your case you need to determine the size of the field that can be done, for example, by printing the original number in a string (sprintf) and using the strlen function.

    
27.04.2017 / 16:02