Java program to convert floating to binary

To convert floating to binary, the Java code is as follows −

Example

Live Demo

import java.io.*;
public class Demo {
static void decimal_to_bin(int n){
int[] bin_num = new int[50];
int i = 0;
while (n > 0){
bin_num[i] = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--)
System.out.print(bin_num[j]);
}
public static void main (String[] args){
int n = 89;
System.out.println("The conversion from floating to binary is ");
decimal_to_bin(n);
}
}

Output

The conversion from floating to binary is
1011001

A class named Demo contains a function named ‘decimal_to_bin’ that converts a given decimal number into a binary number by iterating through every digit of the number and dividing by 2, and taking its remainder, and again dividing the number by 2. In the main function, the number that needs to be converted is defined and the function is called by passing this number as the parameter.