- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Binary to decimal using C#
To convert binary to decimal, here I have used a while loop and found the remainder of the Binary number, which is the input. After that, the remainder is multiplied by the base value and added.
This is what I did to get the decimal value −
while (val > 0) { remainder = val % 10; myDecimal = myDecimal + remainder* baseVal; val = val / 10; baseVal = baseVal * 2; }
Example
Let us see the complete code to convert binary to decimal in C# −
using System; using System.Collections.Generic; using System.Text; namespace Demo { class toBinary { static void Main(string[] args) { int val = 1010, myBinary, remainder; int myDecimal = 0, baseVal = 1; myBinary = val; while (val > 0) { remainder = val % 10; myDecimal = myDecimal + remainder * baseVal; val = val / 10; baseVal = baseVal * 2; } Console.Write("Binary Number : " + myBinary); Console.Write("
Converted to Decimal: " + myDecimal); Console.ReadLine(); } } }
Output
Binary Number : 1010 Converted to Decimal: 10
- Related Articles
- Decimal to Binary conversion using C Programming
- How to convert Decimal to Binary using C#?
- C++ program to Convert a Decimal Number to Binary Number using Stacks
- C# Program to Convert Binary to Decimal
- Decimal to binary conversion using recursion in JavaScript
- C Program for Decimal to Binary Conversion?
- How do we convert binary to decimal and decimal to binary?
- How to convert a number from Decimal to Binary using recursion in C#?
- C++ Program To Convert Decimal Number to Binary
- C# Program to Convert Decimal to Binary\n
- Swapping adjacent binary bits of a decimal to yield another decimal using JavaScript
- How to Convert Decimal to Binary Using Recursion in Python?
- Program for Binary To Decimal Conversion in C++
- Program for Decimal to Binary Conversion in C++
- Convert decimal fraction to binary number in C++

Advertisements