Question: I would like someone to help me on this and kindly explain it me. It's not only the answer that I want but I want to understand it as well. I did my research but it seems that I cannot find the answer similar or related to the question I posted. I am not really good with programming but I would like to know and to learn more how this goes.

Answer: 
In Java the standard way of doing this would be [`Integer.toBinaryString`](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html). However if you wanted to create your own method you could take a look at this:

```
public static String toBinaryString(int n) {
    String s = "";  // you can also use a StringBuilder here
    do {
        s = (n % 2) + s;  // add to front: 0 if n is divisible by 2,
                          // 1 if n is not divisble by 2.
        n /= 2;  // divide n by 2 to obtain next digit of
                 // binary representation in next iteration
    } while (n != 0);
    return s;
}

```

Although this is written in Java, a similar approach can be taken in pretty much any language.

---

This is what `Integer.toBinaryString` boils down to (actually this method calls another helper-method):

```
public static String toBinaryString(int i) {  
    char[] buf = new char[32];
    int charPos = 32;
    do {
        buf[--charPos] = digits[i & 1];
        i >>>= 1;  // i.e. i /= 2
    } while (i != 0);

    return new String(buf, charPos, (32 - charPos));
}

```

where `digits` is defined as

```
final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z' };

```
for c#:

```
int startVal = 7;
int base = 2;
string binary = Convert.ToString(startVal, base); 

```

if you want algorithm in c++:

```
int i, n, a[100], m=0;
while(n!=0)
{
a[m++] = n%2;
n /= 2;
}
for(i=m-1;i>=0; --i)
cout<<a[i];

```