Java Tutorials

Java is a computer programming language. It is a general-purpose, object-oriented, portable, secure and platform-independent language. It is intended to let application developers "write once, run anywhere" (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.  It was initiated in 1991 by James Gosling, Mike Sheridan, and Patric Naughton. Java was initially called Oak after an Oak tree. Later it named as Java.
There are 8 free resources given below. I have given the link, please follow the like. Happy Learning.

One of my favorite destinations of learning Java is Udemy.com, it is one of the largest and biggest online learning platforms in the world. There are several courses on this portal paid and free. It has many Java courses paid and free.

Java Program to Convert a Decimal Number to Binary Number

Following Java program convert Decimal number to Binary number. Enter any integer as an input. Now we convert the given decimal input into a binary number with the help of String. We have taken a number in decimal and then done modulus division by 2, the remainder is added/concatenated to a binary number which is a String variable. The program output is also shown below.

Image from wikimedia.org

Decimal Number to Binary Number

In the program below, a number in Decimal format has been taken from the keyboard and passed to toBinary(int num) method. Where the number is converted by mode division by 2 and added to variable binaryNumber till the number becomes zero. And then return the value.
Source Code:
import java.io.*;
public class DecimalToBinary {

String binaryNumber;        // Calculate the number into Binary as String
int rem;                    // Store the remainder as int
DecimalToBinary()
{
binaryNumber="";
rem=0;
}
// Binary Method
public String toBinary(int num)
{
if (num == 0)
{
return "0";
}

while (num > 0)
{
rem = num % 2;
binaryNumber = rem + binaryNumber;
num = num / 2;
}
return binaryNumber;
}
// Start main method
public static void main(String[] args) throws IOException
{
int decimal;
DecimalToBinary ob = new DecimalToBinary();
System.out.print("Enter a number to convert: ");
String bin = ob.toBinary(decimal); // Decimal to Binary method calling
System.out.println("The binary number representation is " + bin);

} // End of main method
}

Output:
Enter a number to convert: 100
The binary number representation is 1100100
Enter a number to convert: 15
The binary number representation is 1111
Enter a number to convert: 25
The binary number representation is 11001

We can convert decimal to binary in java using Integer.toBinaryString() method also

Source Code:
public class DecimalToBinaryWithMethod
public static void main(String args[])
{
Integer n1 = new Integer(100);
Integer n2 = new Integer(15);
Integer n3 = new Integer(25);
// Convertion using toBinaryString() method
String binary1 = Integer.toBinaryString(n1); // Method toBinaryString(n1)
String binary2 = Integer.toBinaryString(n2);
String binary3 = Integer.toBinaryString(n3);
System.out.println(binary1);
System.out.println(binary2);
System.out.println(binary3);
}

Output:
1100100
1111
11001
In the above program we have used  Integer.toBinaryString() method to convert directly decimal number to binary number and store as String.