Java Program to Converter a Decimal to Binary

You are Here:

Converter a Number from Decimal to Binary

In the following example, we will convert a Decimal Number (500) to Binary Number (111110100).

Tips: It is recommended to use our online Decimal to Binary calculator for better understanding.

Example

Java Compiler
import java.lang.Math; public class myClass { public static void main(String[] args) { int num = 500; int[] arr = new int[50]; int i = -1; int j; while(num != 0) { i++; arr[i] = num % 2; num = num / 2; } System.out.print("Binary number of 500 (decimal) is "); // reverse the array and display for(j=i; j>=0; j--) System.out.print(arr[j]); } }

Output

Binary number of 500 (decimal) is 111110100

Converter any Given Decimal Number to Binary Number

In the following example, we will convert any given decimal number to a binary number.

Example

Java Compiler
import java.util.Scanner; import java.lang.Math; public class myClass { public static void main(String[] args) { Scanner reader = new Scanner(System.in); int num, copyNum, j; int[] arr = new int[50]; int i = -1; System.out.print("Enter a Decimal Number: "); num = reader.nextInt(); copyNum = num; while(copyNum != 0) { i++; arr[i] = copyNum % 2; copyNum = copyNum / 2; } System.out.format("Binary number of %d (decimal) is ", num); // reverse the array and display for(j=i; j>=0; j--) System.out.print(arr[j]); } }

Output

Enter a Decimal Number: 10 Binary number of 10 (decimal) is 1010

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.

We are working to cover every Single Concept in Java.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author