forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvert Hexadecimal to Octal.cpp
95 lines (48 loc) · 1.03 KB
/
Convert Hexadecimal to Octal.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
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int decimalNum=0, octalNum[30], rem, i=0, len=0;
char hexDecNum[10];
cout<<"Enter the Hexadecimal Number: ";
cin>>hexDecNum;
while(hexDecNum[i]!='\0')
{
len++;
i++;
}
len--;
i=0;
while(len>=0)
{
rem = hexDecNum[len];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=70)
rem = rem-55;
else if(rem>=97 && rem<=102)
rem = rem-87;
else
{
cout<<"\nInvalid Hex Digit!";
cout<<endl;
return 0;
}
decimalNum = decimalNum + (rem*pow(16, i));
len--;
i++;
}
i=0;
while(decimalNum != 0)
{
octalNum[i] = decimalNum%8;
i++;
decimalNum = decimalNum/8;
}
cout<<"\nEquivalent Octal Value: ";
for(i=(i-1); i>=0; i--)
cout<<octalNum[i];
cout<<endl;
return 0;
}