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.

How to Convert Binary Number to Decimal and vice-versa?

RUN CODE SNIPPET

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:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022