Monday 10 December 2012

Code for Decimal to Binary

using System;
class DecimalToBinary
{
    public void dispD2B()
    {
        int num;
        Console.Write("Enter a Number : ");
        num = int.Parse(Console.ReadLine());
        int quot;

        string rem = "";

        while (num >= 1)
        {
            quot = num / 2;
            rem += (num % 2).ToString();
            num = quot;
        }

        // Reversing the  value
        string bin = "";
        for (int i = rem.Length - 1; i >= 0; i--)
        {
            bin = bin + rem[i];

        }

        Console.WriteLine("The Binary format for given number is {0}", bin);

    }
    static void Main()
    {
        DecimalToBinary db = new DecimalToBinary();
        db.dispD2B();
        Console.Read();

    }
}

No comments:

Post a Comment