# C Program to Convert Octal Number to Hexadecimal Number

Here is a C program to convert octal number to hexadecimal number system. Octal number system is a base 8 number system using digits 0 and 7 whereas Hexadecimal number system is base 16 number system and using digits from 0 to 9 and A to F. Given an octal number as input from user convert it to hexadecimal number.
For Example:
1652 in Octal is equivalent to 3AA in hexadecimal number system.

## C program to convert a octal number to hexadecimal number

```#include <stdio.h>
#include <string.h>

int main() {
int octalDigitToBinary[8] = {0, 1, 10, 11, 100, 101, 110, 111};
int hexDigitToBinary[16] = {0, 1, 10, 11, 100, 101, 110, 111, 1000,
1001, 1010, 1011, 1100, 1101, 1110, 1111};
char hexDigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'};
char hexadecimalNumber[30];
long long octalNumber, binaryNumber = 0, position;
int digit, fourDigit, i;

/* Take an Octal Number as input from user */
printf("Enter an Octal Number\n");
scanf("%ld", &octalNumber);

position = 1;
/* Finds Binary of the octal number */
while(octalNumber != 0) {
digit = octalNumber % 10;
binaryNumber = (octalDigitToBinary[digit]*position)+binaryNumber;
octalNumber /= 10;
position *= 1000;
}
/* Now convert Binary number to hexadecimal */
position = 0;
while(binaryNumber != 0){
fourDigit = binaryNumber%10000;
for(i = 0; i < 16; i++){
if(hexDigitToBinary[i] == fourDigit){
hexadecimalNumber[position] = hexDigits[i];
break;
}
}

position++;
binaryNumber /= 10000;
}
hexadecimalNumber[position] = '\0';
strrev(hexadecimalNumber);
printf("HexaDecimal Number = %s", hexadecimalNumber);

return 0;
}
```
Output
```Enter an Octal Number
1652
HexaDecimal Number = 3AA
```
```Enter an Octal Number
1234
HexaDecimal Number = 29C
```

Related Topics