C++ Program to Convert Decimal Number to Binary Number

  • Write a C++ program to convert Decimal Number to Binary Number.
  • Write a C++ program to convert Binary Number to Decimal Number.

In below C++ programs we will learn about fundamentals of decimal and binary number system, how to convert decimal numbers to binary numbers and vice-versa. Given a decimal and a binary number we have to convert it to binary and decimal numbers respectively.

Decimal number system is a base 10 number system using digits for 0 to 9 and Binary number system is base 2 number system and uses 0 and 1 digits.
For Example : 10 in decimal number system is equivalent to 1010 in binary number system.

C++ Program to Convert Decimal Number to Binary Number


Algorithm to convert decimal to binary number
  • Divide the input decimal number by 2 and store the remainder.
  • Store the quotient back to the input number variable.
  • Repeat this process till quotient becomes zero.
  • Equivalent binary number will be the remainders in above process in reverse order.
// C++ program to convert decimal numbers to binary numbers
#include <iostream>
using namespace std;
 
long decimalToBinary(long n);

int main() {
    long decimal;
    
 cout <<"Enter a decimal number\n";
    cin >> decimal;
    cout << "Binary number = " << decimalToBinary(decimal);
     
    return 0;
}
 
// Function to convert a decinal number to binary number
long decimalToBinary(long n) {
    int remainder; 
    long binary = 0, i = 1;
  
    while(n != 0) {
        remainder = n%2;
        n = n/2;
        binary= binary + (remainder*i);
        i = i*10;
    }
    return binary;
}
Output
Enter a decimal number
15
Binary number = 1111
Enter a decimal number
9
Binary number = 1001

In above C++ program, we first take an integer as input from user and store it in variable decimal. Then we call decimalToBinary function to convert decimal function to binary number by implementing above mentioned algorithm.


C++ Program to Convert Binary Number to Decimal Number

Algorithm to convert binary to decimal number
  • We multiply each binary digit with 2^i and add them, where i is the position of the binary digit(starting from 0) from right side. Least significant digit is at position 0.

Let's convert 1010 binary number to decimal number
Decimal number = 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 8 + 0 + 2 + 0 = 10

// C++ program to convert binary numbers to decimal numbers
#include <iostream>
#include <cmath>
using namespace std;
 
long binaryToDecimal(long n);
int main() {
    long binary;
    cout << "Enter a binary number\n";
    cin >> binary;
    
    cout << "Decimal number = " << binaryToDecimal(binary);
     
    return 0;
}
 
// Function to convert a binary number to decimal number
long binaryToDecimal(long n) {
    int remainder; 
    long decimal = 0, i=0;
    while(n != 0) {
        remainder = n%10;
        n = n/10;
        decimal = decimal + (remainder*pow(2,i));
        ++i;
    }
    return decimal;
}
Output
Enter a binary number
1001
Decimal number =  9
Enter a binary number
1111
Decimal number =  15

In above program, we first take a binary number as input using cin and store it in a long variable binary. Then we call binaryToDecimal function by passing binary variable as parameter to convert binary number to decimal number by implementing above mentioned algorithm.