Skip to content

Instantly share code, notes, and snippets.

@cruxrebels
Created April 26, 2016 15:54
Show Gist options
  • Save cruxrebels/426768415e5bb36b926b1f027b83a7ce to your computer and use it in GitHub Desktop.
Save cruxrebels/426768415e5bb36b926b1f027b83a7ce to your computer and use it in GitHub Desktop.
Given a number N >= 0, find its representation in binary. Example: if N = 6, binary form = 110 Tags: InterviewBit Math Problems https://www.interviewbit.com/problems/binary-representation/
string Solution::findDigitsInBinary(int A) {
int r;
string result;
if(A==0)
return "0"; //prev used result += to_string(0); return result;
while(A>0)
{
r = A%2;
result += to_string(r); //Or result.push_back((char)('0' + r)); will also work
A /= 2;
}
reverse(result.begin(), result.end());
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment