Posts Binary Representation (InterviewBit)
Post
Cancel

Binary Representation (InterviewBit)

PROBLEM DESCRIPTION

Given a number N >= 0, find its representation in binary.

Example: if N = 6, binary form = 110

InterviewBit

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Solution {

    public String findDigitsInBinary(int A) {

        if(A == 0) return "0";

        StringBuffer sb = new StringBuffer();

        while(A != 0){

            sb.insert(0, A%2);
            A = A/2;

        }

        return sb.toString();

    }

}
This post is licensed under CC BY 4.0 by the author.