C++ Program for Decimal to binary conversion

Program for Decimal to binary conversion

The concept of conversion is very simple.A decimal number can be converted to a binary number by using the repeated division-by-2 method i.e repeatedly divide the decimal number by 2 until no further divisions can be done and the remainder of each division will give us the binary number of the decimal.

Example:9

1) 9/2, Remainder = 1, Quotient = 4,   2) 4/2, Remainder = 0, Quotient = 2                                      3) 2/2, Remainder = 0, Quotient = 1,   4) 1/2, Remainder = 1, Quotient = 0

        9 in decimal = 1001 in binary

Program to Convert decimal to binary

Algorithm:-

  1. User gives an input(decimal number).
  2. The input is stored in an int type variable say decimal.
  3. A function convert is called with decimal as a parameter.
    1. A loop is started
      1. The number is divided by 2 and the remainder is stored.

                               rem = n%2;

                           2. The binary number is constructed using the remainder.

                              binary += rem*i;

                  2. The loop will run until the number is not equal to 0.

                     while (n!=0)

     4. The output is printed. 

C++ Code:-

    
    //C++ Program
    //Decimal to binary conversion
    #include <iostream>
    #include <math.h>
    using namespace std;
    //function to convert decimal to binary
    long convert(int n)
    {
        long binary = 0;
        int i = 1;
        //converting decimal to binary
        while (n!=0)
        {
            int rem = n%2;
            n /= 2;
            binary += rem*i;
            i *= 10;
        }
        return binary;
    }
    //main program
    int main()
    {
        int decimal;
        long binary;
        cout << “Enter a decimal number: “;
        //user input
        cin >> decimal;
        //calling function
        binary = convert(decimal);
        cout << decimal << ” in decimal = “ << binary << ” in binary” << endl ;
        return 0;
    }

 


    Output

    Enter a decimal number: 109
    109 in decimal = 1101101 in binary
coding (3)
  • Highest Common Factor(HCF): C | C++Java | Python
  • Lowest Common Multiple (LCM) : C | C++ | Java | Python
  • Greatest Common Divisor : C | C++ | Java | Python
  • Binary to Decimal to conversion : C | C++ | Java | Python
  • Binary to Octal conversion : C | C++ | Java  | Python
  • Decimal to Binary conversion: C | C++ | Java | Python
  • Decimal to octal Conversion: C | C++ | Java | Python
  • Octal to Binary conversion : C | C++ | Java | Python
  • Octal to Decimal conversion : C | C++ | Java | Python
  • Quadrants in which a given coordinate lies : C | C++ | Java | Python
  • Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
  • Maximum number of handshakes: C | C++ | Java | Python
  • Addition of two fractions: C | C++ | Java | Python
  • Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
  • Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
  • Count possible decoding of a given digit sequence : C | C++ | Java
  • Check whether a character is a vowel or consonant : C | C++ | Java | Python
  • Check whether a character is a alphabet or not : C | C++ | Java | Python
  • Calculate the area of a circle : C | C++ | Java | Python
  • Find the ASCII value of a character : C | C++ | Java | Python
  • Find the prime numbers between 1 to 100 : C | C++ | Java | Python
  • Calculate the number of digits in an integer : C | C++ | Java | Python
  • Convert digit/number to words : C | C++ | Java | Python
  • Counting number of days in a given month of a year: C | C++ | Java | Python
  • Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
  • Finding number of integers which has exactly x divisors: C | C++ | Java | Python
  • Finding Roots of a quadratic equation : C | C++ | Java | Python