C# program to convert floating to binary

CsharpProgrammingServer Side Programming

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
raja
Published on 21-Aug-2018 12:26:42
Advertisements