-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexadecimal-decimal.cpp
More file actions
69 lines (66 loc) · 906 Bytes
/
hexadecimal-decimal.cpp
File metadata and controls
69 lines (66 loc) · 906 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
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
#include <bits/stdc++.h>
#include <cstdint>
#include <ios>
using namespace std;
int hexadecimalToDecimal(string n)
{
int ans = 0;
int x = 1;
int s = n.size();
for (int i = s - 1; i >= 0; i--)
{
if (n[i] >= '0' && n[i] <= '9')
{
ans += x * (n[i] - '0');
}
else if (n[i] >= 'A' && n[i] <= 'F')
{
ans += x * (n[i] - 'A' + 10);
}
x *= 16;
}
return ans;
}
int decimalToBinary(int n)
{
int x = 1;
int ans = 0;
while (x <= n)
{
x *= 2;
}
x /= 2;
while (x > 0)
{
int lastDigit = n / x;
n -= lastDigit * x;
x /= 2;
ans = ans * 10 + lastDigit;
}
return ans;
}
int decimalToOctal(int num)
{
int x = 1;
int ans = 0;
while (x <= num)
{
x *= 8;
}
x /= 8;
while (x > 0)
{
int lastDigit = num / x;
num -= lastDigit * x;
x /= 8;
ans = ans * 10 + lastDigit;
}
return ans;
}
int32_t main()
{
// string n;
int n;
cin >> n;
cout << decimalToOctal(n);
}