-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber System Converter.c
275 lines (184 loc) · 5.58 KB
/
Number System Converter.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//Main Function
#include<stdio.h>
int main(){
menu();
return 0;
}
//Menu section
void menu(){
int i;
printf("Enter:\n");
printf("1 for Decimal to Binary\n");
printf("2 for Decimal to Octal\n");
printf("3 for Decimal to Hexadecimal\n");
printf("4 for Binary to Decimal\n");
printf("5 for Binary to Octal\n");
printf("6 for Binary to Hexadecimal\n");
printf("7 for Octal to Decimal\n");
printf("8 for Octal to Binary\n");
printf("9 for Octal to Hexadecimal\n");
printf("10 for Hexadecimal to Decimal\n");
printf("11 for Hexadecimal to Binary\n");
printf("12 for Hexadecimal to Octal\n");
scanf("%d", &i);
if(i==1){
Decimal_Binary();
}else if(i==2){
Decimal_Octal();
}else if(i==3){
Decimal_Hexadecimal();
}else if(i==4){
Binary_Decimal();
}else if(i==5){
Binary_Octal();
}else if(i==6){
Binary_Hexadecimal();
}else if(i==7){
Octal_Decimal();
}else if(i==8){
Octal_Binary();
}else if(i==9){
Octal_Hexadecimal();
}else if(i==10){
Hexadecimal_Decimal();
}else if(i==11){
Hexadecimal_Binary();
}else if(i==12){
Hexadecimal_Octal();
}
int n;
printf("\n1. Return to Menu: \n");
printf("2. Exit. \n");
scanf("%d", &n);
if(n=1){
menu();
}else{
return 0;
}
}
//Calculation for: Decimal to Binary
void Decimal_Binary(){
int temp, decimal, reminder, binary = 0, i = 1;
printf("Enter a Decimal number: ");
scanf("%d", &decimal);
temp = decimal;
while(decimal != 0){
reminder = decimal % 2;
decimal = decimal /2;
binary = binary + (reminder * i);
i = i * 10;
}
printf("The Binary equivalent of the Decimal number %d is: %d\n\n", temp, binary);
}
//Calculation for: Decimal to Octal
void Decimal_Octal(){ //2
int decimal;
printf("Enter a Decimal number: ");
scanf("%d", &decimal);
printf("The Octal equivalent of the Decimal number %d is: %o\n\n", decimal, decimal);
}
//Calculation for: Decimal to Hexadecimal
void Decimal_Hexadecimal(){ //3
int decimal;
printf("Enter a Decimal number: ");
scanf("%d", &decimal);
printf("The Hexadecimal equivalent of the Decimal number %d is: %x\n\n", decimal, decimal);
}
//Calculation for: Binary to Decimal
void Binary_Decimal(){ //4
int temp, binary, reminder, decimal = 0, i = 1;
printf("Enter a Binary number: ");
scanf("%d", &binary);
temp = binary;
while(binary != 0){
reminder = binary % 10;
binary = binary / 10;
decimal = decimal + (reminder * i);
i = i * 2;
}
printf("The Decimal equivalent of the Binary number %d is: %d\n\n", temp, decimal);
}
//Calculation for: Binary to Octal
void Binary_Octal(){ //5
int temp, binary, reminder, decimal = 0, i = 1;
printf("Enter a Binary number: ");
scanf("%d", &binary);
temp = binary;
while(binary != 0){
reminder = binary % 10;
binary = binary / 10;
decimal = decimal + (reminder * i);
i = i * 2;
}
printf("The Octal equivalent of the Binary number %d is: %o\n\n", temp, decimal);
}
//Calculation for: Binary to Hexadecimal
void Binary_Hexadecimal(){ //6
int temp, binary, reminder, decimal = 0, i = 1;
printf("Enter a Binary number: ");
scanf("%d", &binary);
temp = binary;
while(binary != 0){
reminder = binary % 10;
binary = binary / 10;
decimal = decimal + (reminder * i);
i = i * 2;
}
printf("The Hexadecimal equivalent of the Binary number %d is: %x\n\n", temp, decimal);
}
//Calculation for: Octal to Decimal
void Octal_Decimal(){ //7
int octal;
printf("Enter a Octal number: ");
scanf("%o", &octal);
printf("The Decimal equivalent of the Octal number %o is: %d\n\n", octal, octal);
}
//Calculation for: Octal to Binary
void Octal_Binary(){ //8
int temp, octal, reminder, binary = 0, i = 1;
printf("Enter a Octal number: ");
scanf("%o", &octal);
temp = octal;
while(octal != 0){
reminder = octal % 2;
octal = octal /2;
binary = binary + (reminder * i);
i = i * 10;
}
printf("The Binary equivalent of the Octal number %o is: %d\n\n", temp, binary);
}
//Calculation for: Octal to Hexadecimal
void Octal_Hexadecimal(){ //9
int octal;
printf("Enter a Octal number: ");
scanf("%o", &octal);
printf("The Hexadecimal equivalent of the Octal number %o is: %x\n\n", octal, octal);
}
//Calculation for: Hexadecimal to Decimal
void Hexadecimal_Decimal(){ //10
int hexadecimal;
printf("Enter a Hexadecimal number: ");
scanf("%x", &hexadecimal);
printf("The Decimal equivalent of the Hexadecimal number %x is: %d\n\n", hexadecimal, hexadecimal);
}
//Calculation for: Hexadecimal to Binary
void Hexadecimal_Binary(){ //11
int temp, decimal, hexadecimal, reminder, binary = 0, i = 1;
printf("Enter a Hexadecimal number: ");
scanf("%x", &hexadecimal);
temp = hexadecimal;
while(hexadecimal != 0){
reminder = hexadecimal % 2;
hexadecimal = hexadecimal /2;
binary = binary + (reminder * i);
i = i * 10;
}
printf("The Binary equivalent of the Hexadecimal number %x is: %d\n\n", temp, binary);
}
//Calculation for: Hexadecimal to Octal
void Hexadecimal_Octal(){ //12
int hexadecimal;
printf("Enter Hexadecimal number: ");
scanf("%x", &hexadecimal);
printf("The Octal equivalent of the Hexadecimal number %x is: %o\n\n", hexadecimal, hexadecimal);
}