ADVERTISEMENT
ADVERTISEMENT

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.

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.