Program to convert Octal To Hexadecimal

Problem statement:-  Program to convert Octal To Hexadecimal.

 Data requirement:-

   Input Data:- dec

  Output Data:- Hex

  Additional Data:-i, j, rem

Program in C

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

Code:

//Octal to Hexadecimal
#include <stdio.h>
#include <math.h>
int main ()
{
  int octal,rem,i=0;
  printf ("Enter Octal number: ");
  scanf ("%d", &octal);
  char Hex[100];
    int decimal = 0, sem = 0;

    //Octal to decimal covert
  while(octal!=0)
  {
        decimal=decimal+(octal%10)*pow(8,sem);
        sem++;
        octal=octal/ 10;
  }
  //Decimal to Hexadecimal
   while(decimal!=0)
  {
      rem=decimal%16;
      //Convert Integer to char
      if(rem<10)
        Hex[i++]=rem+48;// 48 Ascii=0
      else
        Hex[i++]=rem+55;//55 Ascii=7
   decimal/=16;
  }

  printf ("Hexadecimal number is:");
  int j;
  for(j=i-1;j>=0;j--)
    printf("%c",Hex[j]);
  return 0;
}

Input/Output:
Enter Octal number: 1234
Hexadecimal number is:29C

Program in C++

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

Code:

//Hexadecimal to Octal
#include<iostream>
#include<cmath>
using namespace std;
int main ()
{
int octal,rem,i=0;
  cout<<"Enter Octal number: ";
  cin>>octal;
  char Hex[100];
    int decimal = 0, sem = 0;

    //Octal to decimal covert
  while(octal!=0)
  {
        decimal=decimal+(octal%10)*pow(8,sem);
        sem++;
        octal=octal/ 10;
  }
  //Decimal to Hexadecimal
   while(decimal!=0)
  {
      rem=decimal%16;
      //Convert Integer to char
      if(rem<10)
        Hex[i++]=rem+48;// 48 Ascii=0
      else
        Hex[i++]=rem+55;//55 Ascii=7
   decimal/=16;
  }

  cout<<"Hexadecimal number is:";
  int j;
  for(j=i-1;j>=0;j--)
    cout<<Hex[j];
  return 0;
}

Input/Output:
Enter Octal number: 2040
Hexadecimal number is:420

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

Code:

import java.util.Scanner;
public class OctalToHexadecimal {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int octal,rem,i=0;
System.out.println("Enter Octal number:");
  octal=cs.nextInt();
  int decimal = 0, sem = 0;
  char Hex[]=new char[100] ;
  
//Octal to decimal covert
  while(octal!=0)
  {
        decimal=(int) (decimal+(octal%10)*Math.pow(8,sem));
        sem++;
        octal=octal/ 10;
  }
  
  //Decimal to Hexadecimal
   while(decimal!=0)
  {
      rem=decimal%16;
      //Convert Integer to char
      if(rem<10)
        Hex[i++]=(char)(rem+48);// 48 Ascii=0
      else
        Hex[i++]=(char)(rem+55);//55 Ascii=7
   decimal/=16;
  }

  System.out.print("Hexadecimal number is:");
  int j;
  for(j=i-1;j>=0;j--)
  System.out.print(Hex[j]);
  cs.close();
}
}

Input/Output:
Enter Octal number:
7463
Hexadecimal number is:F33

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

Code:

i=0
octal=int(input("Enter Octal number:"))
Hex=['0']*50
decimal = 0
sem = 0
#Octal to decimal covert
while octal!=0:
    decimal=decimal+(octal%10)*pow(8,sem);
    sem+=1
    octal=octal// 10
#Decimal to Hexadecimal
while decimal!=0:
    rem=decimal%16
    #Convert Integer to char
    if rem<10:
        Hex[i]=chr(rem+48)#48 Ascii=0
        i+=1
    else:
        Hex[i]=chr(rem+55) #55 Ascii=7
        i+=1
    decimal//=16

print("Hexadecimal number is:")
for j in range(i-1,-1,-1):
    print(Hex[j],end="")

Input/Output:
Enter Octal number:14
Hexadecimal number is:
C

Post a Comment

1 Comments

Please do not Enter any spam link in the comment box