Courses

# Program to convert Decimal to Binary Number

0 Suresh Chand March 28, 2021

In this example, We will learn how to convert decimal number to binary number.

To understand this example, You must have knowledge in following topics

``````#include <iostream>
#include <cmath>

using namespace std;

int convertBinaryToDecimal(long long);

int main()
{
long long n;

cout << "Enter a binary number: ";
cin >> n;

cout << n << " in binary = " << convertBinaryToDecimal(n) << " in decimal";
return 0;
}

int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}``````

The output of above program is

``````Enter a decimal number: 10
Step 1: 10/2, Remainder = 0, Quotient = 5
Step 2: 5/2, Remainder = 1, Quotient = 2
Step 3: 2/2, Remainder = 0, Quotient = 1
Step 4: 1/2, Remainder = 1, Quotient = 0
10 in decimal = 1010 in binary``````

In the above example, We have created one function convertBinaryToDecimal() which converts the decimal number to the binary number.

Subscribe
Notify of
Inline Feedbacks