Created
April 26, 2016 15:54
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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