C# Program To Convert Decimal Number to Binary


Decimal Number To Binary Conversion

This C# program performs the number conversion from decimal number to its equivalent binary number. This program will read a decimal number from the user and stores it modulo by 2 in an array a[] and divide the same number by 2 and stores in same variable 'n'.

Now for the input retrieve the numbers from the array a[] and print or display it on the screen.

Lets have a look on the code...

/* C# program to convert number from decimal to binary...*/
using System;
namespace ConsoleApp1
{
    class NumberConverter
    {
        static void Main(string[] args)
        {
            int i;  //variable i declared to use in for loop
            int[] a = new int[10];  //array a[] is declared to store the values after computation
            Console.Write("Enter the decimal number: ");
            int n = int.Parse(Console.ReadLine());   //getting the decimal value from the user
            for (i = 0; n > 0; i++)
            {
        a[i] = n % 2;
                n = n / 2;
            }
            Console.Write("Its Binary Equivalent is: ");
            for (i = i - 1; i >= 0; i--)
            {
                Console.Write(a[i]);   //displaying the output stored in array a[] 
            }
            Console.Write("\n");   //to get the cursor on new line
            Console.ReadKey();   // hold the program not to terminate till any key is pressed to see output
}
}
}  

Happy coding :)

Related Post:

Comments

Popular posts from this blog

MultiSelection of Item in Recycler View

Upload Image From Android To Php Server

Merge Sort