/** Given an int, determine its binary, octal, decimal representation
*/

public class Convert_2_8_10
{
    /** Given a non-negative integer n, return its binary representation 
    */
    public static String int2binary(int n) {
	if (n == 0) return "0";
	String s = "";
	while (n != 0) {
	    s = (char)('0' + n % 2) + s;
	    n /= 2;
	}
	return s;
    }

    /** Given a non-negative integer n, and an integer b, 1 < b <= 10,
	return the representation of n in base b 
    */
    public static String int2base(int n, int b) {
	assert b > 1 && b <= 10;
	if (n == 0) return "0";
	String s = "";
	while (n != 0) {
	    s = (char)('0' + n % b) + s;
	    n /= b;
	}
	return s;
	// Could you modify it to work also with base 16?
    }

    public static void main(String[] args) {
	System.out.println("The binary representation of 345 is " +
		int2binary(345));
	System.out.println("The binary representation of 345 is " +
		int2base(345, 2));
	System.out.println("The octal representation of 342 is " +
		int2base(345, 8));
	System.out.println("The decimal representation of 345 is " +
		int2base(345, 10));
    }
}
