Wednesday , 13 September 2017
Latest Posts
Home » ICSE Important Programs » Program on Decimal to Binary Number Conversion

# Program on Decimal to Binary Number Conversion

#### 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
{
System.out.print("Enter a decimal number : ");

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.]

## [Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

1. 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 )