# Program to convert Hexadecimal To Octal

Problem statement:- Program to convert Hexadecimal To Octal

## Data requirement:-    Input Data:- dec   Output Data:- octal   Additional Data:-i, j, rem, hex, sem Program in C

Here is the source code of the C program to convert hexadecimal to the octal number system.

Code:

#include<stdio.h>
#include<string.h>
#include<math.h>
int main ()
{
int sizea,i;
printf ("Enter the array size:");
scanf ("%d", &sizea);
char hex[sizea+1];
for (i = 0; i < sizea+1; i++)
{
scanf ("%c", &hex[i]);
}
int value=0;
int decimal=0;
int j=strlen(hex);
j--;
int octal=0,sem=1;
for(i=0;hex[i]!='\0';i++)
{
switch (hex[i])
{
case '0':
value=0;
break;
case '1':
value=1;
break;
case '2':
value=2;
break;
case '3':
value=3;
break;
case '4':
value=4;
break;
case '5':
value=5;
break;
case '6':
value=6;
break;
case '7':
value=7;
break;
case '8':
value=8;
break;
case '9':
value=9;
break;
case 'A':
case 'a':
value=10;
break;
case 'B':
case 'b':
value=11;
break;
case 'C':
case 'c':
value=12;
break;
case 'D':
case 'd':
value=13;
break;
case 'E':
case 'e':
value=14;
break;
case 'F':
case 'f':
value=15;
break;
}
decimal+=value*pow(16,j);
j--;
}
//convert decimal to octal
while(decimal!=0)
{
octal=octal+(decimal%8)*sem;
decimal=decimal/8;
sem=sem*10;
}

printf("Octal Number is: %d",octal);
}

Input/Output:
Enter the array size:2
Octal Number is: 356

Program in C++

Here is the source code of the C++ program to convert a hexadecimal number to octal number.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main ()
{
int sizea,i;
cout<<"Enter the array size:";
cin>>sizea;
char hex[sizea];
for (i = 0; i < sizea; i++)
{
cin>>hex[i];
}
int value=0;
int decimal=0;
int j=sizea;
j--;
int rem,octal=0,sem=1;
for(i=0;hex[i]!='\0';i++)
{
switch (hex[i])
{
case '0':
value=0;
break;
case '1':
value=1;
break;
case '2':
value=2;
break;
case '3':
value=3;
break;
case '4':
value=4;
break;
case '5':
value=5;
break;
case '6':
value=6;
break;
case '7':
value=7;
break;
case '8':
value=8;
break;
case '9':
value=9;
break;
case 'A':
case 'a':
value=10;
break;
case 'B':
case 'b':
value=11;
break;
case 'C':
case 'c':
value=12;
break;
case 'D':
case 'd':
value=13;
break;
case 'E':
case 'e':
value=14;
break;
case 'F':
case 'f':
value=15;
break;
}
decimal+=value*pow(16,j);
j--;
}
//convert decimal to octal
while(decimal!=0)
{
octal=octal+(decimal%8)*sem;
decimal=decimal/8;
sem=sem*10;
}

cout<<"Octal Number is: "<<octal;
}

Input/Output:
Enter the array size:3
Octal Number is: 7632

Program in Java

Here is the source code of the Java program to convert a hexadecimal number to octal number.

Code:

import java.util.Scanner;

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int i;
String hex;
hex=cs.nextLine();

int value=0;
int decimal=0;
int j=hex.length();
j--;
int octal=0,sem=1;
for(i=0;i<hex.length();i++)
{
switch (hex.charAt(i))
{
case '0':
value=0;
break;
case '1':
value=1;
break;
case '2':
value=2;
break;
case '3':
value=3;
break;
case '4':
value=4;
break;
case '5':
value=5;
break;
case '6':
value=6;
break;
case '7':
value=7;
break;
case '8':
value=8;
break;
case '9':
value=9;
break;
case 'A':
case 'a':
value=10;
break;
case 'B':
case 'b':
value=11;
break;
case 'C':
case 'c':
value=12;
break;
case 'D':
case 'd':
value=13;
break;
case 'E':
case 'e':
value=14;
break;
case 'F':
case 'f':
value=15;
break;
}
decimal+=value*Math.pow(16,j);
j--;
}
//convert decimal to octal
while(decimal!=0)
{
octal=octal+(decimal%8)*sem;
decimal=decimal/8;
sem=sem*10;
}

System.out.println("Octal Number is: "+octal);
cs.close();
}
}

Input/Output:
EF
Octal Number is: 357

Program in Python

Here is the source code of the Python Program program to convert a hexadecimal number to octal number without function.

Code:

import math
value=0
decimal=0
j=len(hex)
j-=1
for i in range(0,len(hex)):
if hex[i]>='0' and hex[i]<='9' :
value=(int)(hex[i])
if hex[i]=='A' or hex[i]=='a':
value=10
if hex[i] == 'B' or hex[i] == 'b':
value=11
if hex[i] == 'C' or hex[i] == 'c':
value=12
if hex[i] == 'D' or hex[i] == 'd':
value=13
if hex[i] == 'E' or hex[i] == 'e':
value=14
if hex[i] == 'F' or hex[i] == 'f':
value=15
decimal=decimal+(int)(value*math.pow(16,j))
j-=1
sem=1
octal=0
while(decimal !=0):
octal=octal+(decimal%8)*sem
decimal=decimal//8
sem=int(sem*10)
print("Octal Number is:",octal)

Input/Output: