Skip to content

Commit

Permalink
Merge pull request #474 from santiago177/print-binary
Browse files Browse the repository at this point in the history
Added intToBinary.cpp file, this code prints the binary representation of a given integer
  • Loading branch information
Srikant Singh authored Oct 25, 2018
2 parents 94d0ae8 + 5b0262b commit a07d6cf
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Algorithms/intToBinary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>

using namespace std;

int stringToInt(string s) {
int p = 1;
int n = 0;
for(int i = s.size()-1; i >= 0; i --) {
n += (s[i] - '0') * p;
p *= 10;
}
return n;
}

string intToBinary(int n) {
string bin = "";
while(n > 0) {
bin = (char)((n % 2)+'0') + bin;
n /= 2;
}
return bin;
}

int main(int argc, char** argv) {
/*cout<<argv[1]<<endl;
cout<<argc<<endl;
cout<<stringToInt(argv[1])<<endl;*/
cout<<intToBinary(stringToInt(argv[1]))<<endl;
return 0;
}

0 comments on commit a07d6cf

Please sign in to comment.