**Question:**

Write a Program in Java to input a number in Decimal number system and convert it into its equivalent number in the Binary number system.

**Note:** Binary Number system is a number system which can represent a number in any other number system in terms of 0 and 1 only. This number system consists of only two basic digits i.e. 0 and 1.

For Example: 25 in the Decimal number system can be represented as 11001 in the Binary number system.

**Solution:**

/** * The class Dec2Bin inputs a Decimal number and converts it into its equivalent Binary number * @author : www.javaforschool.com * @Program Type : BlueJ Program - Java */ import java.io.*; class Dec2Bin { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); System.out.print("Enter a decimal number : "); int n=Integer.parseInt(br.readLine()); int r; String s=""; //variable for storing the result char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number system while(n>0) { r=n%2; //finding remainder by dividing the number by 2 s=dig[r]+s; //adding the remainder to the result and reversing at the same time n=n/2; } System.out.println("Output = "+s); } }

**Output:**

Enter a decimal number : 25

Output = 11001

Enter a decimal number : 47

Output = 101111

Enter a decimal number : 6

Output = 110

**Note:**Don’t forget to share these resources and links from our website in your social networking sites with your friends and followers.]

Sir, can we use the following function for decimal to binary conversion in isc practicals?

String bin=Integer.toBinaryString(15)

(Just an example to convert 15 into binary equivalent )

Reply soon sir..

No

Very Good Site !!

This website is very helpful to ICSE tenth standard students!!!!! ThankYOU!!

Is use of int datatype for storing the number inputted by the user is right in this prog as we’re taking decimal value..?

int n=Integer.parseInt(br.readLine());

Yes you can do the program by taking inputs in the form of an integer and not string. But, the above concept is simple as using it we can convert from Decimal to Binary or Octal or Hexa-decimal.

If you take input in integer form, then decimal to hexa-decimal program becomes complicated. This is why we have used the same one method to convert from Decimal to any number system. This makes the program easy to remember.