C++

C++ Program to Convert Decimal to Binary

C++ is a modern object-oriented language using which developers can design different applications. While programming we have to deal with different number systems like decimal, binary, hexadecimal, and octal. Sometimes we need to convert one number system to another. This article provides instructions on the conversion of decimal numbers to binary in C++.

Decimal Numbers

Decimal numbers are a numerical system that is based on the number 10. They use 10 different digits (0-9) to represent any numerical value. For example, any combination of digits, such as 123, 786, 992, and 0 are all decimal numbers.

Binary Numbers

Binary numbers, on the other hand, are based on a numerical system of 2, and use only 2 digits: 0 and 1. Any combination of these digits represents a binary number, such as 1001, 101, 11111, or 101010.

Binary numbers are widely used in computer systems because they can be represented by switches that are either on or off, which makes them a fundamental building block of computer memory and processing.

How to Convert Decimal to Binary in C++

To convert a decimal number to binary in C++ following steps will be followed:

  • Take the input decimal number from the user.
  • Create an empty string to store the binary representation of the number.
  • Divide the decimal number by 2 and append the remainder to the string.
  • Continue dividing the decimal part by 2 until the decimal number becomes 0. After each division, storing the result in a binary output variable.
  • Use a for loop to print converted binary numbers.

Following is the example C++ program that takes user input in decimal and converts it to binary:

#include <iostream>

using namespace std;

int main()

{

  int binaryDigits[10], decimalNumber, index = 0;

cout << "Enter a decimal number to convert to binary: ";

cin >> decimalNumber;

  // Converting decimal number to binary

  while (decimalNumber > 0)

  {

binaryDigits[index] = decimalNumber % 2;

decimalNumber = decimalNumber / 2;

      index++;

  }

cout << "Binary of the given number is: ";

  for (int i = index - 1; i >= 0; i--)

  {

cout << binaryDigits[i];

  }

  return 0;

}

The program started by declaring an array called binaryDigits with 10 elements, an integer variable decimalNumber, and an integer variable index initialized to 0. After that, the user is asked to enter a decimal number, which is read in and stored in decimalNumber.

After that, a while loop will convert decimal to binary. In each iteration of the loop, the remainder of the decimal number divided by 2 is stored in the binaryDigits array at the current index, and the decimal number is divided by 2. The index is then incremented to move to the next element of the array. This will go on until the decimal number becomes 0.

After the binary conversion is complete, a for loop is used to print the binary number. The for loop starts at the highest index of the binaryDigits array (index – 1) and iterates backward to the first element (index 0). The binary digits are printed to the console in reverse order, from the last element of the binaryDigits array to the first.

The following output will appear on the console. The user is asked to enter the Decimal number and its converted binary number is printed.

Conclusion

Decimal numbers are base 10 numbers which are normally used. On the other hand, binary numbers are base 2 numbers that only have two values, either 1 or 0. In C++ we can write code that converts the decimal to a binary number using some simple steps. This article covers the complete code for the conversion of decimal to binary.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.