Your approach is only applicable to a subset of integers.
Because base 6 numbers have more digits than base 10 numbers, there will come a time where the base 10 number will produce a base 6 number that will not fit into an int, resulting in an overflow.
One method is to produce the base 6 number using strings.
A character array is used to hold the number.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
const int maxdigits = 26; /* max number of base 6 digits for a 64 bit int */
int num=123456789, rem = 0;
/* handle negative input */
int tempnum = abs(num);
int lastpos = maxdigits-1;
/* initialize array, and initialize array to spaces */
char res[maxdigits + 1];
memset(res, ' ', maxdigits);
/* null terminate */
res[maxdigits] = 0;
do
{
rem = tempnum % 6;
res[lastpos--] = rem + '0'; /* set this element to the character digit */
tempnum /= 6;
} while (tempnum > 0);
printf("%s%s", (num < 0)?"-":"", res + lastpos + 1); /* print starting from the last digit added */
}
Output:
20130035113