{{ message }}

Instantly share code, notes, and snippets.

# laxman954/ConvertDecimalToBase2TO9.java

Last active Jul 29, 2016
converts number to different base system (base2 - base9)
 package com.lp.mysample; import java.math.BigInteger; /** * @author lperumalm This class converts number to different base system (base2 * - base9) *

Sample Test Data for 16

*
* *
• decimal-base2 for number 16 is 10000
• *
• decimal-base3 for number 16 is 121
• *
• decimal-base4 for number 16 is 100
• *
• decimal-base5 for number 16 is 31
• *
• decimal-base6 for number 16 is 24
• *
• decimal-base7 for number 16 is 22
• *
• decimal-base8 for number 16 is 20
• *
• decimal-base9 for number 16 is 17
• *
*/ public class ConvertDecimalToBase2TO9 { public static void main(String[] args) { decimal2Base(new BigInteger("12345678900000")); } private static void decimal2Base(BigInteger no) { for (int i = 2; i < 9; i++) { BigInteger value = BigInteger.ZERO, factor = BigInteger.ONE, x = no, j = BigInteger.valueOf(i); while (x.compareTo(BigInteger.ZERO) > 0) { value = ((x.mod(j)).multiply(factor)).add(value); x = x.divide(j); factor = factor.multiply(BigInteger.TEN); } System.out.println("decimal-base" + i + " for number " + no + " is " + value); } } }