Wednesday, June 8, 2016

Write a Program to convert Decimal to Binary in C#.NET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication16
{
    class Program
    {
        static void Main()
        {
            int num;
            Console.Write("Enter a Number : ");
            num = int.Parse(Console.ReadLine());
            int q;
 
            string rem = "";
 
            while (num >= 1)
            {
                q = num / 2;
                rem += (num % 2).ToString();
                num = q;
            }
 
 
            string b = "";
            for (int i = rem.Length - 1; i >= 0; i--)
            {
                b = b + rem[i];
 
            }
 
            Console.WriteLine("The Binary num of given number is {0}", b);
 
 
            Console.Read();
 
        }
    }

}

Output :

No comments:

Post a Comment