×

Java Programs

Java Practice

Java program to convert Decimal Number into Binary Number

This program will read Decimal number from the User and convert into its equivalent Binary Number in Java. It is an example of Decimal Number to Binary Number conversion in Java.

Java program to convert Decimal Number into Binary Number

package com.includehelp;

import java.util.Scanner;

/**
 * Program to convert given Decimal number to binary format
 * @author includehelp
 */
public class DecimalToBinary {
    
    static String getBinaryNumber(int decimalNumber){
        StringBuilder binaryStr=new StringBuilder();
        while(decimalNumber>0){
            int r           =decimalNumber%2;
            decimalNumber   =decimalNumber/2;
            binaryStr.append(r);   
        }
        return ( binaryStr.reverse().toString() ); 
    }
    public static void main(String[] arg){
        Scanner sc  =   new Scanner(System.in);
        System.out.print("Enter Decimal Number  : ");
        int decimalNumber =   sc.nextInt();
        
        String binaryNumber = getBinaryNumber(decimalNumber);
        
        System.out.println("Binary Number : "+binaryNumber);
       
    }
    
}

Output

Enter Decimal Number  : 566565
Binary Number : 10001010010100100101

Java Most Popular & Searched Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.