Decimal to Binary Conversion

Decimal to Binary Conversion” is one of the classic programming problem exercise for beginners. Here, we are given a decimal number, entered by user and our task is to write a program to convert the given decimal number to its equivalent binary number.

Example (Decimal to Binary Conversion):

7 -> 111
3 -> 11
8 -> 1000

The steps for decimal to binary conversion are as follows:

  1. Take Binary = 0, iterator = 1.
  2. Store the remainder when the decimal number is divided by 2.
  3. Divide the decimal number by 2.
  4. Binary = Binary + iterator * remainder.
  5. Multiply the iterator by 10.
  6. Repeat the steps from 2 to 5 until decimal number is not 0.
  7. Print the Binary Number. 

C++ Program for Decimal to Binary Conversion is as follows:

/* C++ Program for Decimal to Binary Conversion */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan the Decimal Number */  
    int Decimal;  
    cout<<"Enter the Decimal Number: ";  
    cin>>Decimal;  
      
    /* Converting Decimal to Binary */  
    int Binary = 0;  
    int iterator = 1;  
    int temp = Decimal;  
    while(temp != 0)  
    {  
        int remainder = temp % 2;  
        temp = temp / 2;  
        Binary = Binary + remainder*iterator;  
        iterator = iterator * 10;  
    }  
      
    /*Printing the result */  
    cout<<"The Binary of "<<Decimal<<" is "<<Binary;  
}  
OUTPUT:
Enter the Decimal Number: 7
The Binary of 7 is 111

Related Posts:

  1. Program to convert decimal number to octal number.
  2. Program to convert decimal number to hexadecimal number.
  3. Program to convert hexadecimal number to binary number.
  4. Program to convert hexadecimal number to octal number.
  5. Program to convert hexadecimal number to decimal number.
  6. Program to convert binary number to octal number.
  7. Program to convert binary number to decimal number.
  8. Program to convert binary number to hexadecimal number.
  9. Program to convert octal number to binary number.
  10. Program to convert octal number to decimal number.
  11. Program to convert octal number to hexadecimal number.
  12. Program to check Leap Year.
  13. Program to find Power of the Number.
  14. Program to find Quotient and Remainder.
  15. Program to find largest amongst three numbers.
  16. Program to find factorial of a number.
  17. Program to find GCD of two numbers.
  18. Program to find LCM of two numbers.
  19. Program to check whether entered number is odd or even.
  20. Program to check whether entered number is prime number or not.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *