-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinarytodecimal.cpp
82 lines (78 loc) · 1.31 KB
/
binarytodecimal.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
using namespace std;
void decimalToBinary()
{
int n;
cout<<"enter the decimal number"<<endl;
cin>>n;
int x=1,ans=0;
while(x <= n)
{
x*=2;
}
x/=2;
while(x>0)
{
int lastDigit = n/x;
cout<<"lastDigit"<<lastDigit<<endl;
n -= lastDigit*x;
x /= 2;
cout<<"n"<<n<<endl;
ans = ans*10 + lastDigit;
}
cout<<ans;
}
void decimalToOctal()
{
int n;
cout<<"enter the decimal number"<<endl;
cin>>n;
int x=1,ans=0;
while(x<=n)
{
x *= 8;
}
x /= 8;
while(x>0)
{
int lastDigit = n/x;
n -= lastDigit*x;
x /= 8;
ans = ans*10 +lastDigit;
}
cout<<ans;
}
void decimalToHexadecimal()
{
int n;
cout<<"enter the decimal number"<<endl;
cin>>n;
string s="";
int x=1;
while(x<=n)
{
x *= 16;
}
int lastDigit = 0;
x /= 16;
while(x>0)
{
lastDigit = n/x;
n -= lastDigit*x;
x /= 16;
if(lastDigit >= 0 && lastDigit<=9)
{
s = s + to_string(lastDigit);
}
if(lastDigit >= 10 && lastDigit<=15)
{
char c = 'A' + lastDigit-10;
s.push_back(c);
}
}
cout<<s;
}
int main()
{
decimalToHexadecimal();
}