forked from fineanmol/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecimal_conversion.cpp
More file actions
169 lines (162 loc) · 3.9 KB
/
decimal_conversion.cpp
File metadata and controls
169 lines (162 loc) · 3.9 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
using namespace std;
// Decimal to Binary
void decimal_binary(int decimal) {
cout << "Binary: ";
if (decimal == 0) {
cout << 0 << endl;
return;
}
int bin[32], i = 0;
int temp = decimal;
while (temp > 0) {
bin[i++] = temp % 2;
temp /= 2;
}
for (int j = i - 1; j >= 0; j--)
cout << bin[j];
cout << endl;
}
// Decimal to Octal
void decimal_octal(int decimal) {
cout << "Octal: ";
if (decimal == 0) {
cout << 0 << endl;
return;
}
int oct[32], i = 0;
int temp = decimal;
while (temp > 0) {
oct[i++] = temp % 8;
temp /= 8;
}
for (int j = i - 1; j >= 0; j--)
cout << oct[j];
cout << endl;
}
// Decimal to Hexadecimal
void decimal_hex(int decimal) {
cout << "Hexadecimal: ";
if (decimal == 0) {
cout << 0 << endl;
return;
}
char hex[32];
int i = 0;
int temp = decimal;
while (temp > 0) {
int rem = temp % 16;
if (rem < 10)
hex[i++] = rem + '0';
else
hex[i++] = rem - 10 + 'A';
temp /= 16;
}
for (int j = i - 1; j >= 0; j--)
cout << hex[j];
cout << endl;
}
// Binary to Decimal
int binary_decimal(long binary) {
int decimal = 0, base = 1;
long temp = binary;
while (temp > 0) {
int digit = temp % 10;
if (digit != 0 && digit != 1) {
cout << "Invalid binary number!" << endl;
return -1;
}
temp /= 10;
}
while (binary > 0) {
int last = binary % 10;
decimal += last * base;
base *= 2;
binary /= 10;
}
return decimal;
}
// Octal to Decimal
int oct_dec(int octal) {
int decimal = 0, base = 1;
int temp = octal;
while (temp > 0) {
int digit = temp % 10;
if (digit < 0 || digit > 7) {
cout << "Invalid octal number!" << endl;
return -1;
}
temp /= 10;
}
while (octal > 0) {
int lastDigit = octal % 10;
decimal += lastDigit * base;
base *= 8;
octal /= 10;
}
return decimal;
}
// Hexadecimal to Decimal
int hex_dec(string hex) {
int decimal = 0, base = 1;
int len = hex.length();
for (int i = len - 1; i >= 0; i--) {
char c = hex[i];
int value;
if (c >= '0' && c <= '9')
value = c - '0';
else if (c >= 'A' && c <= 'F')
value = c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
value = c - 'a' + 10;
else {
cout << "Invalid hexadecimal number!" << endl;
return -1;
}
decimal += value * base;
base *= 16;
}
return decimal;
}
int main() {
int num, ch;
string hex;
cout << "\n1: dec to binary\n2: dec to oct\n3: dec to hex\n4: binary to dec\n5: oct to dec\n6: hex to dec\nEnter choice: ";
cin >> ch;
switch (ch) {
case 1:
cout << "Enter number for conversion: ";
cin >> num;
decimal_binary(num);
break;
case 2:
cout << "Enter number for conversion: ";
cin >> num;
decimal_octal(num);
break;
case 3:
cout << "Enter number for conversion: ";
cin >> num;
decimal_hex(num);
break;
case 4:
cout << "Enter number for conversion: ";
cin >> num;
cout << binary_decimal(num) << endl;
break;
case 5:
cout << "Enter number for conversion: ";
cin >> num;
cout << oct_dec(num) << endl;
break;
case 6:
cout << "Enter number for conversion: ";
cin >> hex;
cout << hex_dec(hex) << endl;
break;
default:
cout << "Invalid choice!" << endl;
break;
}
return 0;
}