C# program to convert floating to binary


Let’s say the following is our float −

float n = 50.5f;

Take an empty string to display the binary value and loop until the value of our float variable is greater than 1 −

string a = "";

while (n >= 1) {
   a = (n % 2) + a;
   n = n / 2;
}

Let us see the complete example −

Example

 Live Demo

using System;
using System.IO;
using System.CodeDom.Compiler;

namespace Program {
   class Demo {
      static void Main(string[] args) {

         // float to binary
         Console.WriteLine("float to binary = ");
         float n = 50.5f;
         string a = "";

         while (n >= 1) {
            a = (n % 2) + a;
            n = n / 2;
         }
         Console.Write(a);
      }
   }
}

Output

float to binary =
1.5781251.156250.31250.6251.250.5

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

497 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements