# C Program to Convert Octal Number to Hexadecimal Number

• Write a C program to convert octal number to hexadecimal number system.
• How to convert octal number to hexadecimal number.

#### Required Knowledge

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'};
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){
break;
}
}

position++;
binaryNumber /= 10000;
}
}
Enter an Octal Number

Enter an Octal Number
HexaDecimal Number = 29C