In this tutorial, we will write a program to convert hexadecimal to octal in C. Before that, you must have knowledge of the following topics in C.

**C operator****C while loop****C for loop**

**Hexadecimal** **number**

The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

**Octal number**

The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

Let us go through a program for the Hexadecimal to Octal Conversion in C.

**C Program to Convert Hexadecimal to Octal**

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 | #include <stdio.h> #include <math.h> int main() { int deci = 0, oct[30], rem, i = 0, length = 0; char hexDec[10]; printf("Enter Hexadecimal number: "); scanf("%s", hexDec); //find the length of the number entered while (hexDec[i] != '\0') { length++; i++; } length--; i = 0; while (length >= 0) { rem = hexDec[length]; if (rem >= 48 && rem <= 57) rem -= 48; else if (rem >= 65 && rem <= 70) rem -= 55; else if (rem >= 97 && rem <= 102) rem -= 87; else { printf("The hexadecimal number entered is invalid."); return 0; //exit the program } deci += (rem* pow(16, i)); length--; i++; } i = 0; while (deci != 0) { oct[i] = deci % 8; i++; deci /= 8; } //display printf("Equivalent Octal Value: "); for (i = i - 1; i >= 0; i--) printf("%d", oct[i]); return 0; } |

**Output:**

`Enter Hexadecimal number: FF`

Equivalent Octal Value: 377