Computer Science, asked by arkadutta5498, 11 months ago

Java program for to convert decimal number to binary number

Answers

Answered by yash777star
4

Answer:

import java.util.Scanner;

public class Convert

{

   public static void main(String args[])  

   {

       int n, count = 0, a;

       String x = "";

       Scanner buffer = new Scanner(System.in);

       System.out.print("Enter any decimal number:");

       n = buffer.nextInt();

       while(n > 0)

       {

           a = n % 2;

           if(a == 1)

           {

               count++;

           }

           x = x + "" + a;

           n = n / 2;

       }

       System.out.println("Binary number:"+x);

       System.out.println("No. of 1s:"+count);

   }

}

Explanation:

the basic idea is to use the manual method of conversion of decimals to binary base using string (or basically adding numbers to it) and then to print the string.

Hope this helps.

Similar questions