Java Program to convert Decimal to Binary

Posted on: March 15, 2016

Objectives :

• Decimal to Binary Conversion
• Converting Decimal to Binary
• Converting Decimal Number to Binary format
• Write a program to convert decimal to binary
• Write a program to convert decimal number to binary format
• Write a program to convert positive decimal number to binary format
• Write a program to convert decimal to binary using Scanner class
• Write a program to convert decimal to binary, take input using Scanner class

Java Program to convert Decimal to Binary :

public class DecimalToBinary {

public void toBinary(int number){
int binary[] = new int;
int n = 0;
// Convert Decimal to Binary
while(number > 0){
binary[n++] = number%2;
number = number/2;
}
// Print Binary number
for(int i = n-1;i >= 0;i--){
System.out.print(binary[i]);
}
}

public static void main(String[] args){
DecimalToBinary obj = new DecimalToBinary();
obj.toBinary(14);
}
}

Output :

1110

Java Program to convert Decimal to Binary using Scanner class :

import java.util.Scanner;

public class DecimalToBinary {

public void toBinary(int number){
int binary[] = new int;
int n = 0;

// Convert Decimal to Binary
while(number > 0){
binary[n++] = number%2;
number = number/2;
}

// Print Binary Number
for(int i = n-1;i >= 0;i--){
System.out.print(binary[i]);
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num= scanner.nextInt();
DecimalToBinary obj = new DecimalToBinary();
obj.toBinary(num);

}
}

Output :

Enter a number: 14

1110

1 Response to "Java Program to convert Decimal to Binary" You explained very well by using above simple examples of how to convert decimal number to binary number in java.
thanks for your nice article.

Like This site uses Akismet to reduce spam. Learn how your comment data is processed.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6342121,"math_prob":0.97474056,"size":2936,"snap":"2019-43-2019-47","text_gpt3_token_len":661,"char_repetition_ratio":0.22169168,"word_repetition_ratio":0.32608697,"special_character_ratio":0.24523161,"punctuation_ratio":0.15779817,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9980618,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-10-16T12:44:16Z\",\"WARC-Record-ID\":\"<urn:uuid:5a7ec1cf-ca99-4065-8e1c-d96670eed521>\",\"Content-Length\":\"116395\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:03c85266-d0d2-4aa7-a125-888b0cbd7f76>\",\"WARC-Concurrent-To\":\"<urn:uuid:a9ae1574-91fb-48f4-8704-c1c6860b7dda>\",\"WARC-IP-Address\":\"192.0.78.24\",\"WARC-Target-URI\":\"https://aatul.me/2016/03/15/java-program-to-convert-decimal-to-binary/\",\"WARC-Payload-Digest\":\"sha1:7LLZ3XKREPMA737KM4346VWKQDV62NZN\",\"WARC-Block-Digest\":\"sha1:NVEFBRJBB4QTWTOSGAX3K4EG4J2WAWNA\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-43/CC-MAIN-2019-43_segments_1570986668569.22_warc_CC-MAIN-20191016113040-20191016140540-00295.warc.gz\"}"}