-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbersystemconversion.cpp
111 lines (103 loc) · 1.83 KB
/
numbersystemconversion.cpp
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
#include<iostream>
#include<math.h>
using namespace std;
void bindec(int n)
{
int x=1, ans=0;
while(n>0)
{
int y = n%10;
ans += y*x;
x *=2;
n /= 10;
}
cout<<"Ans = "<<ans<<endl;
}
void octdec(int n)
{
int x=1, ans=0;
while(n>0)
{
int y = n%10;
ans += y*x;
x *=8;
n /= 10;
}
cout<<"Ans = "<<ans<<endl;
}
void hexdec(int n) // ABCDEF not assigned
{
int x=1, ans=0;
while(n>0)
{
int y = n%10;
ans += y*x;
x *=16;
n /= 10;
}
cout<<"Ans = "<<ans<<endl;
}
void decbin(int n)
{
int x=1,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;
}
cout<<"Ans = "<<ans<<endl;
}
void decoct(int n)
{
int x=1,ans=0;
while(x<=n)
x *= 8;
x /= 8;
while(x>0)
{
int lastdigit = n/x;
n -= lastdigit*x;
x /= 8;
ans = ans*10 + lastdigit;
}
cout<<"Ans = "<<ans<<endl;
}
void dechex(int n) // ABCDEF not assigned
{
int x=1,ans=0;
while(x<=n)
x *= 16;
x /= 16;
while(x>0)
{
int lastdigit = n/x;
n -= lastdigit*x;
x /= 16;
ans = ans*10 + lastdigit;
}
cout<<"Ans = "<<ans<<endl;
}
int main()
{
int n, ch;
cout<<"1> Binary to Decimal\n2> Octal to Decimal\n3> Hexadecimal to Decimal\n4> Decimal to Binary\n5> Decimal to Octal\n6> Decimal to Hexadecimal\n";
cout<<"Enter your choice - ";
cin>>ch;
cout<<"Enter the value - ";
cin>>n;
switch(ch)
{
case 1: bindec(n);
case 2: octdec(n);
case 3: hexdec(n);
case 4: decbin(n);
case 5: decoct(n);
case 6: dechex(n);
}
return 0;
}