Subscribe Us

Convert a decimal number into binary number

Ques: Write a C# program to convert a decimal number into binary number. 





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, i;

            int[] a = new int[10];
            Console.Write("Enter the number to change in binary:");
            n = int.Parse(Console.ReadLine());
            for(i=0; n>0; i++)
            {
                a[i] = n % 2;
                n = n / 2;
            }
            Console.Write("Converted Number in Binary is :");
            for(i=i-1; i>=0; i--)
            {
                Console.Write(a[i]);
            }
            Console.ReadKey();
        }

    }
}

If you learned from this post share with others. Happy Sharing!

Post a Comment

0 Comments