codescracker


java

Java Program to Convert Binary to Hexadecimal



« Previous Program Next Program »


Convert Binary to Hexadecimal

To convert binary to hexadecimal in Java programming, you have to ask to the user to enter the number in binary format to convert in into equivalent hexadecimal format, and display the equivalent hexadecimal value on the output screen.

Java Programming Code to Convert Binary to Hexadecimal

Following Java program ask to the user to enter any number in binary format and will convert the number into equivalent hexadecimal format, to provide the equivalent hexadecimal value on the screen::

/* Java Program Example - Binary to Hexadecimal Conversion */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int binnum, rem;
        String hexdecnum="";
        
        // digits in hexadecimal number system
        
        char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Binary Number : ");
        binnum = scan.nextInt();
		
        // converting the number in hexadecimal format
        while(binnum>0)
        {
            rem = binnum%16;
            hexdecnum = hex[rem] + hexdecnum;
            binnum = binnum/16;
        }
		
        System.out.print("Equivalent Hexadecimal Value of " +binnum+ " is :\n");
        System.out.print(hexdecnum);
            
    }
}

When the above Java Program is compile and executed, it will produce the following result:

java program convert binary to hexadecimal

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test