Search

# Decimal to Binary in C++

Decimal is normal mathematics & Binary is in the form of 0,1,01

In this tutorial we are going to see how to convert decimal to binary

Table Conversion

Decimal

8421 Rule - Binary

0

0000

1

0001

2

0010

3

0011

4

0100

5

0101

6

0110

7

0111

8

1000

9

1001

It is just the basic conversion, Now suppose if we have to do the conversion of 99999, Then it will be quite difficult to convert manually. So, now we are going to see how to use a program for faster conversion.

Algorithm –

1. Divide the number by 2 through % (modulus operator) and store the remainder in array

2. Divide the number by 2 through / (division operator)

3. Repeat the step 2 until the number is greater than zero

Program for Decimal to Binary Conversion

• #include <iostream>

• using namespace std;

• int main()

• {

• int a, n, i;

• cout<<"Enter the number to convert: ";

• cin>>n;

• for(i=0; n>0; i++)

• {

• a[i]=n%2;

• n= n/2;

• }

• cout<<"Binary of the given number= ";

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

• {

• cout<<a[i];

• }

• }

Output –

Enter the number to convert: 1

Binary of the given number= 0001

For Video Tutorials Subscribe our Youtube Channel

---------------------------------------------------------------------------------------------------------------------------

Previous Page Next Page

Any Doubt ? Feel free to write a comment

Download C++ Projects

Join us on LinkedIn – Great Place of Networking

Follow us on More Together – Facebook for regular updates

Mandatory to join Telegram group for regular job updates

Mandatory to join whatsapp group for regular job updates

Other C++ Programs

For Other Tutorials – Click Here

Tags:

34 views0 comments

### Related Posts

See All

#### Number to Character Conversion in C++

Program to implement Number to Character Conversion in C++ Firstly see what we are going to do ? Like this 0 - zero 1 – one 2 – two 3 – three 4 – four 5 – five 6 – six 7 – seven 8 – eight 9 – nine How

#### Factorial of a number in C++

In this tutorial we are going to see how to find a factorial of number using C++ program Firstly let’s see a example to understand it better n! = n + (n-1) + (n-2) ! n>=1 3! = 3 *2*1 = 6 Program to fi

#### Fibonacci Triangle in C++

Program to implement Fibonacci Triangle in C++ #include <iostream> using namespace std; int main() { int a=0,b=1,i,c,n,j; cout<<"Enter the limit: "; cin>>n; for(i=1; i<=n; i++) { a=0; b=1; cout<<b<<"\