C++ Program to Convert Decimal to Binary

Here the program converts 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 whose whole number part and the fractional part are separated by a decimal point.

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, 10012 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 1310 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 from the last remainder to the first remainder, i.e.(MSB to LSB). Hence,
1310 = 11012.

C++ program to convert a decimal number into a binary number

The user is asked to enter the decimal number. The program has to convert it into its equivalent 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, if the condition evaluates to be 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 a for loop is used


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


       cout<


The value of binary num[]gets printed from its 0th index.

Algorithm

Step 1: Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type 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