# C++ Program for Binary to decimal conversion

## Program for Binary to decimal conversion

The concept of converting a binary number to a decimal number is very simple , extract the digits of the binary number (start from right for convenience )and multiply the digits with the proper base(power of 2) lastly add each of the results and we will get a converted number as an output.

Example: 10101

Decimal number = 1*(2^4)+ 0*(2^3) + 1*(2^2) + 0*(2^1) + 1*(2^0)=16 + 0 + 4 +0+1

= 21

## Algorithm:-

1. User gives an input(binary number)
2. The input is stored in a long type variable say binary.
3. A function convert()  is called with binary as parameter.
1. Digits of binary are extracted one by one starting from right.

int rem = n%10;

2. Extracted digits are multiplied with proper base i.e. power of 2.

int res = rem*pow(2,i);

3. After multiplying each digit with proper base the results are added and stored in another variable say decimal.

decimal += res;

4. The output is printed.

### C++ Code:-

//C++ Program
//Convert binary to decimal
#include <iostream>
#include <math.h>
using namespace std;
//function to convert binary to decimal
int convert(long n)
{
int i = 0,decimal0;
//converting binary to decimal
while (n!=0)
{
int rem = n%10;
n /= 10;
int res = rem * pow(2,i);
decimal += res;
i++;
}
return decimal;
}
//main program
int main()
{
long binary;
cout << “Enter binary number: “;
cin >> binary;
cout << binary << ” in binary = “ << convert(binary<< ” in decimal”;
return 0;
}

Output

Enter binary number: 10110
10110 in binary = 22 in decimal

• Highest Common Factor(HCF): C | C++Java | Python
• Lowest Common Multiple (LCM) : C | C++ | Java | Python
• Greatest Common Divisor : C | C++ | Java | Python
• Binary to Decimal to conversion : C | C++ | Java | Python
• Binary to Octal conversion : C | C++ | Java  | Python
• Decimal to Binary conversion: C | C++ | Java | Python
• Decimal to octal Conversion: C | C++ | Java | Python
• Octal to Binary conversion : C | C++ | Java | Python
• Octal to Decimal conversion : C | C++ | Java | Python
• Quadrants in which a given coordinate lies : C | C++ | Java | Python
• Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
• Maximum number of handshakes: C | C++ | Java | Python
• Addition of two fractions: C | C++ | Java | Python
• Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
• Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
• Count possible decoding of a given digit sequence : C | C++ | Java
• Check whether a character is a vowel or consonant : C | C++ | Java | Python
• Check whether a character is a alphabet or not : C | C++ | Java | Python
• Calculate the area of a circle : C | C++ | Java | Python
• Find the ASCII value of a character : C | C++ | Java | Python
• Find the prime numbers between 1 to 100 : C | C++ | Java | Python
• Calculate the number of digits in an integer : C | C++ | Java | Python
• Convert digit/number to words : C | C++ | Java | Python
• Counting number of days in a given month of a year: C | C++ | Java | Python
• Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
• Finding number of integers which has exactly x divisors: C | C++ | Java | Python
• Finding Roots of a quadratic equation : C | C++ | Java | Python