Java – Decimal To Binary, Decimal To Octal And Decimal To HexaDecimal Conversion

In this tutorial i am going to show you how to convert “Decimal to Binary”, “Decimal to Octal” and “Decimal to Hexadecimal” Check the Java Program below.

Java Program To Convert Decimal To Binary :-

import java.util.Scanner;
public class DecimalToBinary
{   
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        //Taking input from the user
        
        System.out.println("Enter The Decimal Number : ");
        
        int inputNumber = sc.nextInt();
        
        //Copying inputNumber into copyOfInputNumber
        
        int copyOfInputNumber = inputNumber;
        
        //Initializing binary to empty string
        
        String binary = "";
        
        //Defining rem to store remainder
        
        int rem = 0;
        
        
        while (inputNumber > 0)
        {
            rem = inputNumber % 2;
            
            binary =  rem + binary;
            
            inputNumber = inputNumber/2;
        }
        
        System.out.println("Binary Equivalent of "+copyOfInputNumber+" is "+binary);
    }
}

Output :

Enter The Decimal Number :
50
Binary Equivalent of 50 is 110010

Java Program To Convert Decimal To Octal :-

importjava.util.Scanner;
publicclassDecimalToOctal
{   
    publicstaticvoidmain(String[] args)
    {
        Scanner sc = newScanner(System.in);
        
        //Taking input from the user
        
        System.out.println("Enter The Decimal Number : ");
        
        intinputNumber = sc.nextInt();
        
        //Copying inputNumber into copyOfInputNumber
        
        intcopyOfInputNumber = inputNumber;
        
        //Initializing octal to empty string
        
        String octal = "";
        
        //Defining rem to store remainder
        
        intrem = 0;
        
        
        while(inputNumber > 0)
        {
            rem = inputNumber%8;
            
            octal =  rem + octal;
            
            inputNumber = inputNumber/8;
        }
        
        System.out.println("Octal Equivalent of "+copyOfInputNumber+" is "+octal);
    }
}
Output :
Enter The Decimal Number :
1000
Octal Equivalent of 1000 is 1750
Java Program To Convert Decimal To Hexa Decimal :-
importjava.util.Scanner;
publicclassDecimalToHexaDecimal
{   
    publicstaticvoidmain(String[] args)
    {
        Scanner sc = newScanner(System.in);
        
        //Taking input from the user
        
        System.out.println("Enter The Decimal Number : ");
        
        intinputNumber = sc.nextInt();
        
        //Copying inputNumber into copyOfInputNumber
        
        intcopyOfInputNumber = inputNumber;
        
        //Initializing hexa to empty string
        
        String hexa = "";
        
        //Digits in HexaDecimal Number System
        
        charhexaDecimals[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        
        //Defining rem to store remainder
        
        intrem = 0;
        
        
        while(inputNumber > 0)
        {
            rem = inputNumber%16;
            
            hexa =  hexaDecimals[rem] + hexa;
            
            inputNumber = inputNumber/16;
        }
        
        System.out.println("HexaDecimal Equivalent of "+copyOfInputNumber+" is "+hexa);
    }
}

Output :

Enter The Decimal Number :
2000
HexaDecimal Equivalent of 2000 is 7D0

 

 

Leave a Reply

avatar
  Subscribe  
Notify of