Write a program to convert Octal to Hexadecimal number system


This Java program converts an octal number to hexadecimal using the built-in parseInt() method and toHexString() method.

  • The program first declares a Scanner object to read input from the user. It then declares a String variable oct to store the octal number entered by the user, an int variable dec to store the decimal equivalent of the octal number, and a String variable hex to store the hexadecimal equivalent.
  • The program then prompts the user to enter an octal number, reads it using the Scanner object, and stores it in the oct variable.
  • Next, the program uses the parseInt() method to convert the octal number to decimal. The method takes two arguments: the octal number to be converted (as a String), and the base of the number system to convert from (in this case, 8 for octal). The method returns the decimal equivalent as an int, which is stored in the dec variable.
  • Finally, the program uses the toHexString() method to convert the decimal number to hexadecimal. The method takes one argument: the decimal number to be converted (as an int). The method returns the hexadecimal equivalent as a String, which is stored in the hex variable.
  • The program then prints the hexadecimal equivalent of the octal number using System.out.print(). Note that there is a space after the text "Hexadecimal Number:" before the actual hexadecimal number.

Source Code

import java.util.Scanner;
public class Octal_Hexadecimal
{
	public static void main(String args[])
	{
		String oct, hex;
		int dec;
		Scanner in = new Scanner(System.in);
 
		System.out.print("Enter The Octal Number : ");
		oct = in.nextLine();
 
		dec = Integer.parseInt(oct, 8);
		hex = Integer.toHexString(dec);
 
		System.out.print("Hexadecimal Number: "+ hex);
	}
}
 

Output

Enter The Octal Number : 14
Hexadecimal Number: c

Example Programs