• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Tuesday, August 11, 2020

Decimal to Binary Conversion in C# with Examples

 Admin     August 11, 2020     C#, Interview Question, Programming, Programs     No comments   

In this article, I am going to discuss the Decimal to Binary Conversion in C# with some examples. Please read our previous article where we discussed the Sum of Digits of a given number Program in C# in many different ways. In C#, we can easily convert any decimal number (base-10 (i.e. 0 to 9)) into binary number (base-2 (i.e. 0 or 1)). As part of this article, we are going to discuss the following pointers.
  1. What are Decimal Numbers?
  2. What are Binary Numbers?
  3. How to Convert Decimal to Binary in C#?
What are Decimal Numbers?
The Decimal numbers are the numbers whose base is 10. That means the decimal numbers are range from 0 to 9. Any combination of such digits (digits from 0 to 9) is a decimal number such as 2238, 1585, 227, 0, 71, etc.

What are Binary Numbers?
The Binary numbers are the numbers whose base is 2. That means the binary numbers can be represent using only two digits i.e. 0 and 1. So, any combination of these two numbers (i.e. 0 and 1) is a binary number such as 101, 10o1, 111111, 10101, etc.

Let us have a look which shows the decimal number along with its binary representation.
Decimal to Binary Conversion in C#

Algorithm: Decimal to Binary Conversion

Step1: First, divide the number by 2 through modulus (%) operator and store the remainder in an array
Step2: Divide the number by 2 through division (/) operator.
Step3: Repeat step 2 until the number is greater than zero.


Program: Decimal to Binary Conversion in C#
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the Decimal Number : ");
            int number = int.Parse(Console.ReadLine());
            int i;
            int[] numberArray = new int[10];
            for (i = 0; number > 0; i++)
            {
                numberArray[i] = number % 2;
                number = number / 2;
            }
            Console.Write("Binary Represenation of the given Number : ");
            for (i = i - 1; i >= 0; i--)
            {
                Console.Write(numberArray[i]);
            }
            Console.ReadKey();
        }
    }
}
Output:
Decimal to Binary Conversion Program in C#

Let us see another way of doing the conversion from Decimal to Binary in C#.
In the following program, we created a string variable to hold the binary representation of the Decimal number.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the Decimal Number : ");
            int number = int.Parse(Console.ReadLine());
            
            string Result = string.Empty;
            for (int i = 0; number > 0; i++)
            {
                Result = number % 2 + Result;
                number = number / 2;
            }
            Console.WriteLine($"Binary Represenation of the given Number : {Result}");
            
            Console.ReadKey();
        }
    }
}
Output:
Conversion from Decimal to Binary in C#

In the next article, I am going to discuss the Binary to Decimal Conversion Program in C# with some examples. Here, in this article, I try to explain the Decimal to Binary Conversion in C# with some examples.

Summary:
I hope this post will be helpful to understand Decimal to Binary Conversion in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • Creating and Working with Database
    Hi friends! In our last post we have seen different approaches how we can connect with SQL Server Management Studio(SSMS). Today, We are g...
  • Entity Types in Entity Framework
    In this article, I am going to discuss the Entity Types in Entity Framework in detail. Please read our previous article where we discussed...
  • Introduction to Entity Framework
    Before .NET 3.5 as a developer, we often used to write ADO.NET code to perform CRUD operation with the underlying database. For this, we ne...
  • Filters in ASP.Net MVC
    Hi friends! Today we are going to discuss about using a very important feature of MVC i.e. “Filters“ . Filters are a unique feature of Asp...
  • Data Parallelism in C#
    Introduction: Hi, in this blog we are going to discuss a very important feature of C# that is data parallelism. Data parallelism means the ...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • ASP.NET Web API Basic Authentication
    In this article, I am going to discuss how to implement the ASP.NET Web API Basic Authentication step by step with an example. Please read...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape