-2

"can you reverse the number" is the number already reverse by using Mod(%). My question, can it be reversed to normal?

For example, if you enter the number "2552" it'll change to "2+5+5+2", which is correct, but, when you enter another number like "4125" it will change to "5+2+1+4" instead of "4+1+2+5"

Ok, I just entered the programming world, a newcomer

With the "if" can it add "+" without exceeding the number like "4+1+2+5+" there are "+" after "5", how can I delete this extra "+"?

#include <stdio.h>
main(){
int a, b, h=0;
    printf("Enter the number : ");
    scanf("%d",&a);

    printf("%d = ", a);

    while(a != 0)
    {
        b=a%10;
        a=a/10;
        printf("%d",b);
        if(b != a)
        {
            printf("+");
        }
        h=h+b;
    }
    printf(" = %d\n", h);

}
Tom
  • 248
  • 1
  • 6
  • 16

2 Answers2

1

Instead of scanning for an actual number, you can scan a string from the user. Strings are easy to reverse:

char num[5]; // Has the input
char rev[5]; // Will have the reverse
int len = strlen(num);
rev[len] = 0; // End the string
for(int i = 0; i < len; i++){
    rev[i] = num[len-i-1];
}
Felix
  • 2,548
  • 19
  • 48
  • This code is a little hard to follow, so I wasn't paying attention to the right things. Those buffers are scary small, though. – tadman Jan 16 '18 at 16:19
  • @tadman Their size can be increased via substituting the '5' with a number that is greater in size ;) – Felix Jan 16 '18 at 16:21
  • 1
    Sarcasm aside, there's an extremely high probability someone will type in a number over 5 characters long. Why not 255? Or 1024? Or something someone's less likely to pop by pure accident? – tadman Jan 16 '18 at 16:23
  • @tadman Yes, that might be appropriate, but is in no way essential to the example I provided. Also, +1 for using powers of two for buffer sizes (although 256 is the *nitpick* right size). – Felix Jan 16 '18 at 16:26
  • 255 has a long tradition in the database world, it's the longest length an 8-bit variable length indicator can handle, and 1024 is a good default for a generic "buffer". This of course glosses over how dangerous C can be if not kept on a very tight leash when it comes to strings in general. – tadman Jan 16 '18 at 16:30
  • @tadman Cool, but wouldn't the buffer size still be 256? As it is null-terminated, the length would be 255. – Felix Jan 16 '18 at 16:31
  • In the database context it's effectively a Pascal-style string so there's no null-terminator, and null values are not significant. It takes the form of one byte length field, (up to) 255 bytes of data. – tadman Jan 16 '18 at 16:33
  • 1
    @tadman Okay, interesting.. Haven't done a lot with databases yet. But thanks! – Felix Jan 16 '18 at 16:35
0

You can go backwards by taking the highest digits in your number first instead of the lowest, e.g. like so:

int currentLog10;
int powerOfTenForCurrentDigit;

...

while(a != 0)
{
  currentLog10 = (int)log10(a);

  powerOfTenForCurrentDigit = (int)pow(10, currentLog10);

  b = a/powerOfTenForCurrentDigit;
  a = a - b * powerOfTenForCurrentDigit;

  ...
}

How does it work:

  • log10 of 4125 will give us 3.
  • b is set to 4125 devided by 1000 (10^3) = 4.
  • a is set to 4125 - 4 * 1000 = 125
  • Next step log10 of 125 gives 2, we devide by 10^2 (100) and so on.
  • Last step 5 gives 0, 5 devided by 10^0 (1) gives 5, a becomes 5 - 5 * 1, which is 0, we are done.

You have to be careful in edge cases, where log10 returns something like (n-1).999999999 instead of n, but it shouldn't happen for small numbers as are entered in your program. Maybe add some sanity check for the input.

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43