Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Convert Binary to Decimal
Firstly, set the binary value −
int num = 101;
Now assign the binary to a new variable −
binVal = num;
Till the value is greater than 0, loop through the binary number and base value like this,
while (num > 0) {
rem = num % 10;
decVal = decVal + rem * baseVal;
num = num / 10;
baseVal = baseVal * 2;
}
Example
The following is the code to convert binary to decimal.
using System;
using System.Collections.Generic;
using System.Text;
namespace Demo {
class MyApplication {
static void Main(string[] args) {
int num, binVal, decVal = 0, baseVal = 1, rem;
num = 101;
binVal = num;
while (num > 0) {
rem = num % 10;
decVal = decVal + rem * baseVal;
num = num / 10 ;
baseVal = baseVal * 2;
}
Console.Write("Binary Number: "+binVal);
Console.Write("
Decimal: "+decVal);
Console.ReadLine();
}
}
}
Output
Binary Number: 101 Decimal: 5
Advertisements