-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin_convert.cpp
More file actions
41 lines (33 loc) · 907 Bytes
/
bin_convert.cpp
File metadata and controls
41 lines (33 loc) · 907 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[]){
int new_base = 2;
int remainder, offset;
int b10_input, dividend;
string result, remainder_str;
if(argc < 2){
printf("Base 10 to Binary Conversion\n");
cout << "Enter Base 10 Input: ";
cin >> b10_input;
}
else {
b10_input = stoi(argv[1]);
}
while(b10_input > 0){
remainder = b10_input % new_base;
if(remainder != 0){
offset = b10_input - remainder;
b10_input = offset / new_base;
}
else{
b10_input = b10_input / new_base;
}
remainder_str = to_string(remainder);
result = result + remainder_str;
}
reverse(result.begin(), result.end());
cout << "Binary Result: " << result << endl;
return 0;
}