-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_6_binary_decimal_converter.cpp
63 lines (49 loc) · 1.02 KB
/
2_6_binary_decimal_converter.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <math.h>
using std::cin;
using std::cout;
using std::endl;
using std::pow;
/*
Binary and decimal converter without using arrays (or strings)
*/
int main()
{
int dec = 0;
int integerSize = 32;
cout << "Type in decimal number to convert to binary:\n";
cin >> dec;
cin.ignore();
int temp = dec;
cout << "The binary of " << dec << " is:\n";
int c = 0;
int n = -1;
int digit = 0;
while (temp)
{
n++;
temp /= 2;
}
temp = dec;
do
{
c = pow(2, n--);
digit = (temp / c) % 2;
cout << digit;
} while(n >= 0);
cout << "\nType in binary number to convert to decimal:\n";
char ch = '0';
dec = 0;
int digitCount = 0;
while(ch != 10 || digitCount >= integerSize)
{
ch = cin.get();
if (ch == ' ' || ch == 10) continue;
dec = dec*2 + ch - '0';
digitCount++;
}
cout << "The decimal number is:\n";
cout << dec;
cin.get();
return 0;
}