Q:

Write a Java program to convert a decimal number to binary numbers

0

Write a Java program to convert a decimal number to binary numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
public class Javaexcercise {
      public static void main(String args[])
    {
        int decimalnum, quot, i=1, j;
        int bin_num[] = new int[100];
        Scanner scan = new Scanner(System.in);
 
        System.out.print("Enter a Decimal Number : ");
        decimalnum = scan.nextInt();
 
        quot = decimalnum;
 
        while(quot != 0)
        {
            bin_num[i++] = quot%2;
            quot = quot/2;
        }
 
        System.out.print("Binary number is: ");
        for(j=i-1; j>0; j--)
        {
            System.out.print(bin_num[j]);
        }
        System.out.print("\n");
    }
}

Result:

Enter a Decimal Number : 20

Binary number is: 10100

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java programming language Basic programs with examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to convert a binary number to... >>
<< Write a Java program to swap two variables...