java program to convert decimal number to binary 26 count number of 1s

Solutions on MaxInterview for java program to convert decimal number to binary 26 count number of 1s by the best coders in the world

showing results for - "java program to convert decimal number to binary 26 count number of 1s"
Facundo
27 May 2018
1// decimal to binary conversion in java
2import java.util.Scanner;
3public class DecimalBinaryDemo
4{
5   public static void main(String[] args)
6   {
7      int number, count = 0, temp;
8      String strConvert = "";
9      Scanner sc = new Scanner(System.in);
10      System.out.println("Enter a decimal number : ");
11      number = sc.nextInt();
12      // decimal to binary java
13      while(number > 0)
14      {
15         temp = number % 2;
16         if(temp == 1)
17         {
18            count++;
19         }
20         strConvert = strConvert + " " + temp;
21         number = number / 2;
22      }
23      System.out.println("Decimal to binary in java : " + strConvert);
24      System.out.println("Number of 1s : " + count);
25      sc.close();
26   }
27}