java

Java Program to Convert Decimal to Binary

In this tutorial, we are going to see how to write a java program to convert a decimal number to a binary number in Java using the toBinaryString() method. The java.lang.Integer.toBinaryString() method returns a string representation of the integer argument as an unsigned integer in base 2.
 

Java Program to Convert Decimal to Binary :
import java.util.Scanner;

class Main
{
    public static void main(String args[])
    {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter a decimal number : ");
      //read the decimal number
      int nbr = sc.nextInt();

      String str = Integer.toBinaryString(nbr);
      System.out.println("The binary number is : "+str);
    }
}

Output:

Enter a decimal number : 25
The binary number is : 11001
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *