In this example, i’ll show you How to Convert Binary to Decimal in C++.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | // C++ program to convert binary to decimal #include <iostream> using namespace std; // Function to convert binary to decimal int binaryToDecimal(int n) { int num = n; int dec_value = 0; // Initializing base value to 1, i.e 2^0 int base = 1; int temp = num; while (temp) { int last_digit = temp % 10; temp = temp / 10; dec_value += last_digit * base; base = base * 2; } return dec_value; } // Driver program to test above function int main() { int num; cout<<"Enter Binary Number : "; cin>>num; cout << binaryToDecimal(num) << endl; } |
Output: