Java - Recursion Program - Convert a base 10 number to any Base

后端 未结 5 581
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 07:30

I am trying to convert a base 10 number to any base by using conversion. Right now this is the code I have came up with. I have a sad feeling this may be completely wrong. The

5条回答
  •  执念已碎
    2021-02-06 07:39

    I just finished doing this problem for a comp sci class. I had to solve this recursively:

    public static String convert(int number, int base)
    {
        int quotient = number / base;
        int remainder = number % base;
    
        if (quotient == 0) // base case
        {
            return Integer.toString(remainder);      
        }
        else
        {
            return convert(quotient, base) + Integer.toString(remainder);
        }            
    }
    

提交回复
热议问题