-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtod.cpp
More file actions
109 lines (103 loc) · 2.11 KB
/
btod.cpp
File metadata and controls
109 lines (103 loc) · 2.11 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;
int binaryToDecimal(int n){
// ip=101 , op=5
// modulo with 10 then will get last digit , multiply it with 2
int ans = 0;
int x = 1;
while (n>0)
{
ans += x *( n % 10);
x *= 2;
n /= 10;
}
return ans;
}
int octalToDecimal(int n){
// modulo with 10 then will get last digit , multiply it with 8
int ans = 0;
int x = 1;
while (n>0)
{
ans += x *(n % 10);
x *= 8;
n /= 10;
}
return ans;
}
int hexadecimalToDecimal(string n){
//find size of given n and compare with 0 to 9 and A to F
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');
/* code */
}else if(n[i]>='A'&&n[i]<='F')
{
ans += x * (n[i] - 'A' + 10);
}
x *= 16;
/* code */
}
return ans;
}
int decToBin(int n){
//first modulo and divide by 2
//exp ip=5, op=101
int ans = 0;
int x = 1;
while (n>0)
{
ans += (n % 2) * x;
n = n / 2;
x = x * 10;
}
return ans;
}
int decToOct(int n){
//first modulo and divide by 8
//exp input=100 , output=144
int ans = 0;
int x = 1;
while (n>0)
{
ans += (n % 8) * x;
n = n / 8;
x = x * 10;
}
return ans;
}
int main(){
int n;
cin >> n;
cout << decToOct(n) << endl;
return 0;
}
//decial to hexadecimal
// int main()
// {
// int n, rem, i=0;
// char ans[50];
// cout<<"Enter the Decimal Number: ";
// cin>>n;
// while(n!=0)
// {
// rem = n%16;
// if(rem<10)
// rem = rem+48;
// else
// rem = rem+55;
// ans[i] = rem;
// i++;
// n = n/16;
// }
// cout<<"\nEquivalent Hexadecimal Value: ";
// for(i=i-1; i>=0; i--)
// cout<<ans[i];
// cout<<endl;
// return 0;
// }