Skip to content

Commit

Permalink
Added intToBinary.cpp file, this code prints the binary representatio…
Browse files Browse the repository at this point in the history
…n of a given integer
  • Loading branch information
Santiago committed Oct 24, 2018
1 parent 2bf5215 commit 5b0262b
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 5b0262b

Please sign in to comment.