└── Decimal to Binary /Decimal to Binary: -------------------------------------------------------------------------------- 1 | class Main { 2 | 3 | public static void main(String[] args) { 4 | 5 | // decimal number 6 | int num = 19; 7 | System.out.println("Decimal to Binary"); 8 | 9 | // call method to convert to binary 10 | long binary = convertDecimalToBinary(num); 11 | 12 | System.out.println("\n" + num + " = " + binary); 13 | } 14 | 15 | public static long convertDecimalToBinary(int n) { 16 | 17 | long binaryNumber = 0; 18 | int remainder, i = 1, step = 1; 19 | 20 | while (n!=0) { 21 | remainder = n % 2; 22 | System.out.println("Step " + step++ + ": " + n + "/2"); 23 | 24 | System.out.println("Quotient = " + n/2 + ", Remainder = " + remainder); 25 | n /= 2; 26 | 27 | binaryNumber += remainder * i; 28 | i *= 10; 29 | } 30 | 31 | return binaryNumber; 32 | } 33 | } 34 | --------------------------------------------------------------------------------