-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign7-3.c
More file actions
93 lines (92 loc) · 1.49 KB
/
assign7-3.c
File metadata and controls
93 lines (92 loc) · 1.49 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
// Number and Base Conversions
#include<stdio.h>
void Bin(int num)
{
int binary[32];
int index=0;
if(num==0)
{
printf("0\n");
return;
}
while(num>0)
{
binary[index++]=num%2;
num/=2;
}
// Print binary number in reverse
for(int i=index-1;i>=0;i--)
{
printf("%d",binary[i]);
}
printf("\n");
}
void Oct(int num)
{
int oct[32];
int index=0;
if(num==0)
{
printf("0\n");
return;
}
while(num>0)
{
oct[index++]=num%8;
num/=8;
}
// Print octal number in reverse
for(int i=index-1;i>=0;i--)
{
printf("%d",oct[i]);
}
printf("\n");
}
void Hex(int num)
{
char hex[32];
int index=0;
if(num==0)
{
printf("0\n");
return;
}
while(num>0)
{
int remainder=num % 16;
if (remainder<10)
hex[index++]=remainder+'0'; // Convert to character '0'-'9'
else
hex[index++]=remainder-10+'A'; // Convert to character 'A'-'F'
num/=16;
}
// Print hexadecimal number in reverse
for(int i=index-1;i>=0;i--)
{
printf("%c",hex[i]);
}
printf("\n");
}
int main()
{
int n;
int base;
scanf("%d %d",&n,&base);
if(base==2)
{
Bin(n);
}
else if(base==8)
{
Oct(n);
}
else if(base==16)
{
Hex(n);
}
else
{
printf("Wrong Base");
}
return 0;
}