Learn Programming and technology with ease @ developerpublish.com

HomeC++C++ Program to Convert Binary Number to Decimal and vice-versa

# C++ Program to Convert Binary Number to Decimal and vice-versa

In this post, you will learn how to Convert Binary Number to Decimal and vice-versa using C++.

This lesson will teach you how to Convert Binary Number to Decimal and vice-versa, with a recursion statement, mathematical operations, loop statements and decision making statement using the C++ Language. Letâ€™s look at the below source code.

## Source Code

```#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<<"\nDecimal 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```

The statementsÂ #include<iostream>, using namespace stdÂ are the main factors that support the function of the source code. The void keyword is used when the function does not have a return value.

Now we can look into the working and layout of the codeâ€™s function.

1. In this code we have two sections one to convert a Binary to a Decimal and one to convert a Decimal to a Binary.
2. The first section is we execute the Decimal to a Binary function.
3. Declare theÂ voidÂ function with the integer n â€“Â int n.Â Declare the integersÂ binaryNumberÂ with the limit as 100, num = n andÂ i = 0.
4. Open aÂ while loopÂ with the condition with the condition `(n > 0)` . The loop statement has the following statements and functions
{ binaryNumber[i] = n % 2; n = n / 2; i++; }Here we find theÂ reminderÂ of n and 2, then assign the value to `binaryNumber[i]` Using theÂ assignment operatorÂ assign the value of n divided by 2. Finally we increment the valueÂ i.
5. Then display the output statement usingÂ coutÂ function followed by aÂ for loopÂ with the condition `(int j = i - 1; j >= 0; j--)`and the output statement to display the answer stored inÂ binaryNumber[j]Â with anÂ endlÂ statement.
6. The second section we execute the Binary to a Decimal.
7. Declare theÂ mainÂ function and declare the integers and assign the values as follows : int decimalNumber = 0,Â int base = 1, int temp = nÂ
8. Declare a while loop. The loop statementÂ has the following functions
{ int lastDigit = temp % 10; temp = temp/10; decimalNumber += lastDigit*base; base = base*2; }Here we find theÂ reminderÂ of temp and 10, and assign the value toÂ lastDigit declared as an integer. We divide the value of temp and 10 and reassign the value in temp
`decimalNumber += lastDigit*base` Â this function is executed where theÂ AND assignment operatorÂ is used with the value of the product of lastDigit and base, Â and finally the product ofÂ base and 2Â  and the answer is assigned to the base.
9. Then finally the output statement is displayed using theÂ coutÂ statement.
10. Declare theÂ int mainÂ function, to enter the values to be used in the functions. In this code we include the numbers in the code rather than getting it from the user.
11. DecimalToBinary with the number 23 and BinaryToDecimalÂ with the numberÂ 10101,Â and return the valueÂ o.

Note: The â€˜ << endl â€˜ in the code is used to end the current line and move to the next line and â€˜\nâ€™ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

Share:

### You May Also Like

#### C++ Program to Generate Multiplication Table

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

#### C++ Program to Display Fibonacci Sequence

In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

#### C++ Program to Find GCD of two Numbers

In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
• C++
• January 30, 2022