
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Convert Binary Number to Decimal and vice-versa
In a computer system, the binary number is expressed in the binary numeral system while the decimal number is in the decimal numeral system. The binary number is in base 2 while the decimal number is in base 10.
Examples of decimal numbers and their corresponding binary numbers are as follows −
Decimal Number | Binary Number |
---|---|
10 | 01010 |
7 | 00111 |
25 | 11001 |
16 | 10000 |
A program that converts the binary numbers into decimal and the decimal numbers into binary is as follows.
Example
#include <iostream> using namespace std; void DecimalToBinary(int n) { int binaryNumber[100], num=n; int i = 0; while (n > 0) { binaryNumber[i] = n % 2; n = n / 2; i++; } cout<<"Binary form of "<<num<<" is "; for (int j = i - 1; j >= 0; j--) cout << binaryNumber[j]; cout<<endl; } int BinaryToDecimal(int n) { int decimalNumber = 0; int base = 1; int temp = n; while (temp) { int lastDigit = temp % 10; temp = temp/10; decimalNumber += lastDigit*base; base = base*2; } cout<<"Decimal form of "<<n<<" is "<<decimalNumber<<endl;; } int main() { DecimalToBinary(23); BinaryToDecimal(10101); return 0; }
Output
Binary form of 23 is 10111 Decimal form of 10101 is 21
In the program given above, there are two functions DecimalToBinary and BinaryToDecimal. These convert the number from decimal to binary and binary to decimal respectively.
In the DecimalToBinary function, the binary value of the decimal number n is stored in the array binaryNumber[]. A while loop is used and the result of the n modulus 2 operation is stored in binaryNumber[] for each iteration of the loop. This is shown using the following code snippet.
while (n > 0) { binaryNumber[i] = n % 2; n = n / 2; i++; }
After this, the binary number is displayed using a for loop. This is shown as follows.
cout<<"Binary form of "<<num<<" is "; for (int j = i - 1; j >= 0; j--) cout << binaryNumber[j];
In the function, BinaryToDecimal(), a while loop is used to convert the binary number into decimal number. The LastDigit contains the last bit of the temp variable. The base contains the base value such as 2, 4, 6, 8 etc. The DecimalNumber contains the sum of the previous DecimalNumber value and the product of the LastDigit and base.
All this is demonstrated using the following code snippet −
while (temp) { int lastDigit = temp % 10; temp = temp/10; decimalNumber += lastDigit*base; base = base*2; }
In the main() function, the DecimalToBinary() and BinaryToDecimal() functions are called. This is shown as follows.
DecimalToBinary(23); BinaryToDecimal(10101);
- Related Articles
- C++ Program to convert Octal Number to Decimal and vice-versa
- C++ Program to Convert Binary Number to Octal and vice-versa
- How to convert binary to decimal and vice versa in Perl?
- How to Convert Decimal Number to Binary/Octal/Hex Number or Vice Versa in Excel?
- Binary to decimal and vice-versa in Python
- Convert from any base to decimal and vice versa in C++
- C++ Program To Convert Decimal Number to Binary
- C++ Program to Convert Km/hr to miles/hr and vice versa
- Java Program to convert binary number to decimal number
- Python program to convert decimal to binary number
- C++ program to Convert a Decimal Number to Binary Number using Stacks
- C# Program to Convert Binary to Decimal
- Converting string to number and vice-versa in C++
- Golang program to convert the arraylist into string and vice-versa
- How to Convert Letter to Number or Vice Versa in Excel?
