Convert the number decimal to binary in Java

 Question 10)

Write a Program to convert decimal to binary.



Answer:-

               import java.util.Scanner;
public class DTB {
   public static void main(String[] args) {
    Scanner ss = new Scanner(System.in);
    System.out.println("Enter decimal number to change in binary number : ");
    int num = ss.nextInt();
    int i=0;
    int binary [] = new int [500];
    while ( num!=0 ) {
      binary[i] = num % 2;
      num = num/2;
      i++;
    }
    System.out.println("The binary value is : ");
    for ( int j=i-1; j>=0; j-- ) {
      System.out.print(" "+ binary[j]);
    }
   } 
}

Post a Comment

1 Comments