C++ Program to Convert Decimal to Binary


January 31, 2023, Learn eTutorial
1157

In this C++ program, we convert the decimal number entered by the user to its corresponding binary number.

Decimal number

In algebra, a decimal number can be defined as a number that has the base 10. means the numbers from 0 to 9.

Binary number

A number system where a number is represented by using only two digits (0 and 1) with a base of 2 is called a binary number system. For example,112 is a binary number.

How to convert a decimal number into a binary number?

To convert a decimal number into its equivalent binary number, we divide the decimal number by 2 each time, till we get 0 as a dividend. Let us take an example to convert 13 into a binary number.

  • 13 ÷ 2: 6 and remainder 1
  • 6 ÷ 2: 3 and remainder 0
  • 3 ÷ 2: 1 and remainder 1
  • 1 ÷ 2: 0 and remainder 1

Now we take the bits of the remainder from the last to the first, i.e.(MSB to LSB). Hence, 1310 = 11012.

How to write a C++ program to convert a decimal number into a binary number?

display a message for user to enter input. The program has to convert this decimal input into its binary value. Here we are using three variables decimalnum, binarynum[20], and i. initially the value of i is set to 0. When the user enters the decimal number, it gets stored in decimalnum. Now a while loop is evaluated with the condition decimalnum!=0. 


while(decimalnum!=0)
{
   binarynum[i] = decimalnum%2;
   i++;
   decimalnum = decimalnum/2;
}


Here, when the condition evaluates true, the program flow goes inside the loop and decimalnum%2 gets initialized to binarynum[i]. The value ofgets incremented. decimalnum/2 is initialized to decimalnum. Now decimalnum = decimalnum/2; The control flow goes back and evaluates the while loop again and the process repeats until the condition of the while loop evaluates to be false. After ending the while loop, we use another for loop for printing the binary number in reverse order.

Algorithm

Step 1: Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the main function; int main().

Step 4: Declare two integer type variables decimalnum, binarynum[20], i=0;

Step 5: Ask the user to enter a number;

Step 6: Get the number into the variable decimalnum;

Step 7: Evaluate a while loop with condition decimalnum!=0

Step 8: If true then compute decimalnum%2 and store the value to binarynum[i];

Step 9: increment the value of i;

Step 10: Update the variable decimalnum by decimalnum/2;

Step 11: Continue step 7 to step 10 until the condition goes wrong;

Step 12: Print the binarynum[] by using a for loop;

Step 13:Exit;

C++ Source Code

                                          #include<iostream>
using namespace std;
int main()
{
    int decimalnum, binarynum[20], i=0;
    cout<<"Enter the decimal Number: ";
    cin>>decimalnum;
    while(decimalnum!=0)
    {
        binarynum[i] = decimalnum%2;
        i++;
        decimalnum = decimalnum/2;
    }
    cout<<"\nEquivalent Binary Value: ";
    for(i=(i-1); i>=0; i--)
        cout<<binarynum[i];
    cout<<endl;
    return 0;
}
                                      

OUTPUT

Enter the decimal Number: 125
Equivalent Binary Value: 1111101