
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# Program to Convert Decimal to Binary
Let’ s say you have set the decimal to be −
decVal = 34; Console.WriteLine("Decimal: {0}", decVal);
Use the ToString() method for the values you get as a binary number for the decimal value −
while (decVal >= 1) { val = decVal / 2; a += (decVal % 2).ToString(); decVal = val; }
Now set a new empty variable to display the binary number using a loop −
string binValue = "";
Example
You can try to run the following code to convert decimal to binary in C#.
using System; using System.Collections.Generic; using System.Text; namespace Demo { class MyApplication { static void Main(string[] args) { int decVal; int val; string a = ""; decVal = 34; Console.WriteLine("Decimal: {0}", decVal); while (decVal >= 1) { val = decVal / 2; a += (decVal % 2).ToString(); decVal = val; } string binValue = ""; for (int i = a.Length - 1; i >= 0; i--) { binValue = binValue + a[i]; } Console.WriteLine("Binary: {0}", binValue); Console.Read(); } } }
Output
Decimal: 34 Binary: 100010
- Related Articles
- C# Program to Convert Binary to Decimal
- C++ Program To Convert Decimal Number to Binary
- C program to convert decimal fraction to binary fraction
- Haskell Program to convert Decimal to Binary
- Haskell Program to convert Binary to Decimal
- Swift Program to convert Decimal to Binary
- Swift Program to convert Binary to Decimal
- Java Program to convert from decimal to binary
- Python program to convert decimal to binary number
- C++ Program to Convert Binary Number to Decimal and vice-versa
- Java program to convert decimal number to binary value
- Java program to convert binary number to decimal value
- Java Program to convert binary number to decimal number
- Convert decimal to binary number in Python program
- How to convert Decimal to Binary using C#?

Advertisements