forked from ravya1108/hactoberfest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Octal_Hexadecimal_without_Function.cpp
98 lines (98 loc) · 2.31 KB
/
Octal_Hexadecimal_without_Function.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
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int octalNum, rev=0, rem, chk=0, hex=0, mul=1, i=0, k=0;
char binaryNum[40] = "", hexnum[40];
cout<<"Enter any Octal Number: ";
cin>>octalNum;
while(octalNum!=0)
{
rem = octalNum%10;
if(rem>7)
{
chk++;
break;
}
rev = (rev*10) + rem;
octalNum = octalNum/10;
}
if(chk==0)
{
octalNum = rev;
while(octalNum!=0)
{
rem = octalNum%10;
switch(rem)
{
case 0: strcat(binaryNum, "000");
break;
case 1: strcat(binaryNum, "001");
break;
case 2: strcat(binaryNum, "010");
break;
case 3: strcat(binaryNum, "011");
break;
case 4: strcat(binaryNum, "100");
break;
case 5: strcat(binaryNum, "101");
break;
case 6: strcat(binaryNum, "110");
break;
case 7: strcat(binaryNum, "111");
break;
}
octalNum = octalNum/10;
}
while(binaryNum[k]!='\0')
k++;
chk=1;
k--;
while(k>=0)
{
if(binaryNum[k]=='0')
rem = 0;
else
rem = 1;
hex = hex + (rem*mul);
if(chk%4==0)
{
if(hex<10)
hexnum[i] = hex+48;
else
hexnum[i] = hex+55;
mul = 1;
hex = 0;
chk = 1;
i++;
}
else
{
mul = mul*2;
chk++;
}
k--;
}
if(chk!=1)
hexnum[i] = hex+48;
if(chk==1)
i--;
cout<<"\nEquivalent Hexadecimal Value = ";
chk = 0;
for(i=i; i>=0; i--)
{
if(hexnum[i]=='0' && chk==0)
{
chk++;
continue;
}
else
cout<<hexnum[i];
}
}
else
cout<<"\nInvalid Octal Digit "<<rem;
cout<<endl;
return 0;
}