Menu
Topics Index
...
`

Integer to Binary Conversion


If you need explanation Read this topic

If you need Answer Take test on this topic

Integer to Binary Conversion
What will be the output of the following program?
public class Convert {

    public void printBinaryFormat(int dec) {
        int bin[] = new int[15];
        int temp = 0;
        while (dec > 0) {
            bin[temp++] = dec % 2;
            dec = dec / 2;
        }
        System.out.print("After binary conversion: ");
        for (int i = temp - 1; i >= 0; i--) {
            System.out.print(bin[i]);
        }
    }

    public static void main(String a[]) {
        Convert dec = new Convert();
        dec.printBinaryFormat(8489);
    }
}

A. After binary conversion: 10000100101001
B. After binary conversion: 10000110001100
C. After binary conversion: 10000100100100
D. Compilation Error or Runtime Error
Topic: Static and Non Static Variables - Static and Non Static Methods

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

A

Posted by Vijay Raichura    2014-07-02 13:03:20


ans is A, here whie loop iterates until dec becomes zero,in each iteration it finds dec%2 value and stores in bin array by incrementing temp var,and dec = dec/2 is executed,for loop prints the bin array in reverse order.

Posted by Raviteja Daggupati    2014-07-02 13:33:29


© meritcampus 2019

All Rights Reserved.

Open In App