- C++ Programming Examples
- C++ Programming Examples
- C++: Hello World
- C++: Get Input
- C++: Print Integer
- C++: Add two numbers
- C++: Add, Sub, Multiply, Div
- C++: Add Digits
- C++: Find Average and Percentage
- C++: Find Arithmetic Mean
- C++: Sum of n Natural Numbers
- C++: Sum of n Numbers
- C++: Square's Area and Perimeter
- C++: Rectangle's Area and Perimeter
- C++: Triangle's Area and Perimeter
- C++: Area and Circumference
- C++: Find Simple Interest
- C++: Fahrenheit to Celsius
- C++: Celsius to Fahrenheit
- C++: Print Prime Numbers
- C++: Reverse a Number
- C++: Swap Two Numbers
- C++: Print Multiplication Table
- C++: Find Factorial of a Number
- C++: Find Factors of a Number
- C++: Find HCF and LCM
- C++: Create a Calculator
- C++: Count Digits in a Number
- C++: First and Last Digit Sum
- C++: Product of Number Digits
- C++: Sum of Squares of Digits
- C++: Interchange Digits of Number
- C++ if-else Programs
- C++: Check Even or Odd
- C++: Check Prime or Not
- C++: Check Alphabet or Not
- C++: Check Vowel or Not
- C++: Check Leap Year or Not
- C++: Check Reverse equals Original
- C++: Check Perfect Number
- C++: Check Palindrome or Not
- C++: Check Armstrong or Not
- C++: Divisibility Test
- C++: Find Labor Wage
- C++: Find Discounted Price
- C++: Find Shipping Charge
- C++: Find Telephone Bills
- C++: Calculate Student Grade
- C++: Largest of Two Numbers
- C++: Largest of Three Numbers
- C++ Number Conversion
- C++: Decimal to Binary
- C++: Decimal to Octal
- C++: Decimal to Hexadecimal
- C++: Binary to Decimal
- C++: Binary to Octal
- C++: Binary to Hexadecimal
- C++: Octal to Decimal
- C++: Octal to Binary
- C++: Octal to Hexadecimal
- C++: Hexadecimal to Decimal
- C++: Hexadecimal to Binary
- C++: Hexadecimal to Octal
- C++ Pattern Programs
- C++: Pattern Programs
- C++: Print Diamond Pattern
- C++: Print Floyd's Triangle
- C++: Print Pascal's Triangle
- C++ Array Programs
- C++: 1D Array Program
- C++: Linear Search
- C++: Binary Search
- C++: Largest Element in an Array
- C++: Smallest Element in an Array
- C++: Find Second Largest Element
- C++: Find Second Smallest Element
- C++: Sum of All Elements
- C++: Multiply All Elements
- C++: Element in Even Position
- C++: Element in Odd Position
- C++: Print Even Numbers in Array
- C++: Print Odd Numbers in Array
- C++: Count Even or Odd Numbers
- C++: Sum of Even or Odd Numbers
- C++: Count Positive, Negative, Zero
- C++: Reverse an Array
- C++: Insert an Element
- C++: Delete an Element
- C++: Merge two Arrays
- C++: Bubble Sort
- C++: Selection Sort
- C++: Insertion Sort
- C++: Common Elements
- C++: 2D Array Programs
- C++: Add Two Matrices
- C++: Subtract Two Matrices
- C++: Transpose Matrix
- C++: Multiply Two Matrices
- C++: 3D Array Programs
- C++ String Programs
- C++: Print String
- C++: Find String Length
- C++: Compare Two Strings
- C++: Copy String
- C++: String Concatenation
- C++: Reverse a String
- C++: Delete Vowels from a String
- C++: Delete a Word from a String
- C++: Count Characters in a String
- C++: Count Words in a String
- C++: Frequency of Words
- C++: Remove Spaces from Strings
- C++: Sort a String
- C++: Uppercase to Lowercase
- C++: Lowercase to Uppercase
- C++: Swap Two Strings
- C++: Check the Anagram or Not
- C++: Capitalize All Words in a String
- C++: Get Numbers from a String
- C++ File Programs
- C++: Read a File
- C++: Write Content to a File
- C++: Append Data to a File
- C++: Read and Display File
- C++: Copy a File
- C++: Merge Two Files
- Count Characters in a File
- C++: Capitalize Every Word
- C++: List Files in Directory
- C++: Delete a File
- C++: Encrypt and Decrypt a File
- C++ Misc Programs
- C++: Print ASCII Value
- C++: Add Binary Numbers
- C++: Generate Random Numbers
- C++: Print a Smiling Face
- C++: Days into Years and Months
- C++: Add Two Numbers using Pointer
- C++: Print Fibonacci Series
- C++: Generate Armstrong Numbers
- C++: Find nCr and nPr
- C++: Get IP Address
- C++: Print Date and Time
- C++: Shutdown and Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
C++ Program to Convert Binary to Decimal
In this article, you will learn and get code on binary to decimal conversion with and without using functions in C++.
But before going through the program, if you are not aware of the steps, formula, or logic used behind the conversion of binary to decimal, you can refer to the section on binary to decimal conversion. You will learn everything there in a very short period of time.
Binary to Decimal without a Function in C++
To convert any number entered by the user (in the binary number system) to its equivalent decimal value in C++ programming, you have to ask the user to enter the binary number first. And then apply the logic and convert it into its equivalent decimal value, as shown in the program given below.
Let's have a look at the program first; I will explain it later on.
#include<iostream> using namespace std; int main() { int binnum, decnum=0, i=1, rem; cout<<"Enter any Binary Number: "; cin>>binnum; while(binnum!=0) { rem = binnum%10; decnum = decnum + (rem*i); i = i*2; binnum = binnum/10; } cout<<"\nEquivalent Decimal Value = "<<decnum; cout<<endl; return 0; }
This program was built and runs under the Code::Blocks IDE. Here is its sample run:
Now supply any input as a binary number, say 1111010, and press the ENTER key to see its equivalent value in decimal, as shown in the output given below:
Program explained with a dry run
The dry run of the above program with user input 1111010 goes in such a way that:
- decnum=0, i=1, and binnum=1111010 (entered by the user) are the initial values.
- The condition binnum!=0 (inside the while loop) or 1111010!=0 evaluates to be true.
- Therefore, program flow goes inside the loop.
- And binnum%10, 1111010%10, or 0 is assigned to rem. Now rem=0
- Similarly, decnum+(rem*i) or 0+(0*1) or 0 gets initialized to decnum. Now decnum=0
- After that, i*2 or 1*2 or 2 is initialized to i. Now i=2
- And at last, binnum/10 or 1111010/10 or 111101 gets initialized to binnum. Now binnum=111101
- After executing all four statements of the while loop (for the first time), we have decnum=0, i=2, and binnum=111101.
- Program flow goes back to the condition of the while loop.
- Again, the condition binnum!=0 or 111101!=0 is evaluated to be true, therefore the program flow again goes inside the loop.
- And binnum%10, 111101%10, or 1 is assigned to rem. Now rem=1
- Similarly, decnum+(rem*i) or 0+(1*2) or 2 gets initialized to decnum. Now decnum=2
- And i*2 or 2*2 or 4 is assigned to i. Now i=4
- And at last, binnum/10 or 111101/10 or 11110 gets initialized to binnum. Now binnum=11110
- Now process from step no. 9 to 14 with the new values of decnum, i, and binnum.
- decnum=2, i=8, and binnum=1111 are now our values.
- Go to step no. 15.
- Now we have decnum=10, i=16, and binnum=111.
- Go to step no. 15.
- Now we have decnum=26, i=32, and binnum=11.
- Go to step no. 15.
- Now we have decnum=58, i=64, and binnum=1.
- Go to step no. 15.
- Now we have decnum=122, i=128, and binnum=0.
Dry Run in Tubular Form
while loop evaluation | rem | decnum | i | binnum |
---|---|---|---|---|
1st Evaluation | 0 | 0 | 2 | 111101 |
2nd Evaluation | 1 | 2 | 4 | 11110 |
3rd Evaluation | 0 | 2 | 8 | 1111 |
4th Evaluation | 1 | 10 | 16 | 111 |
5th Evaluation | 1 | 26 | 32 | 11 |
6th Evaluation | 1 | 58 | 64 | 1 |
7th Evaluation | 1 | 122 | 128 | 0 |
You can also print the value of all four variables, say, rem, decnum, i,, and binnum, during the execution of the while loop by placing the following statement:
cout<<"\nrem\tdecnum\ti\tbinnum\n";
just before the while loop. And the following statement:
cout<<rem<<"\t"<<decnum<<"\t"<<i<<"\t"<<binnum<<endl;
just after the fourth statement of while loop's body. Now, with user input of 1111010, output looks like this:
Binary to decimal conversion using a function in C++
This program converts binary to decimal using a user-defined function, BinToDec(). It takes a binary number as its argument and returns its equivalent decimal value.
This program uses the pow() function. It takes two arguments; the first argument is known as the base, whereas the second is known as the exponent. Therefore, pow(2, 5) means 25, which equals 32.
#include<iostream> #include<math.h> using namespace std; int BinToDec(int bin); int main() { int binnum, decnum; cout<<"Enter any Binary Number: "; cin>>binnum; decnum = BinToDec(binnum); cout<<"\nEquivalent Decimal Value = "<<decnum; cout<<endl; return 0; } int BinToDec(int bin) { int dec=0, i=0, rem; while(bin!=0) { rem = bin%10; dec = dec + rem*pow(2,i); i++; bin = bin/10; } return dec; }
This program produces the same output as the previous program. You can check on your own with a dry run.
The same program in different languages
- C Binary to Decimal Conversion
- Java Binary to Decimal Conversion
- Python Binary to Decimal Conversion
« Previous Program Next Program »