# I need help for storing the splitted characters of two integers into each other by character

8 Views
``# include  int main(){     int a,b;     scanf("%d %d",&a,&b);     while (a > 0) {         int digit = a % 10;         //printf("%d ",digit); for testing         a = a - digit;         a /= 10;     }     while (b > 0) {         int digit = b % 10;         //printf("%d ",digit); for testing         b = b - digit;         b /= 10;     } } ``

This code takes two integers with the same lengths(a,b), splits them into their characters, now the question is how to print it like this for example(a=123 , b= 798 --> result = 17-29-38 Save the digits in arrays. Then you can loop over the arrays to print the digits at the end.

Also, you don't need to subtract `digit` before dividing by 10, since division discards the remainder.

``````# include  #define MAXDIGITS 20  int main(){     int a,b;     int adigits[MAXDIGITS], bdigits[MAXDIGITS];     int digits = 0;      scanf("%d %d",&a,&b);     while (a > 0) {         int digit = a % 10;         //printf("%d ",digit); for testing         adigits[digits++] = digit;         a /= 10;     }     digits = 0;     while (b > 0) {         int digit = b % 10;         //printf("%d ",digit); for testing         bdigits[digits++] = digit;         b /= 10;     }     while (--digits >= 0) {         printf("%d%d", adigits[digits], bdigits[digits]);         if (digits != 0) {             putchar('-');         }     }     putchar('
'); } ``````
see demo