C++ program to convert a decimal number to binary number

Home » C++ » Programs » Decimal number to binary number

// C++ program to convert a decimal number to binary number
 
#include <iostream>
using namespace std;
 
// function to convert decimal to binary
void DecToBinary(int n)
{
    // array to store binary number
    int binaryNum[1000];
 
    // counter for binary array
    int i = 0;
    while (n > 0) {
 
        // storing remainder in binary array
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }

    // printing binary array in reverse order
    for (int j = i - 1; j >= 0; j--)
        cout << binaryNum[j];
}
 
int main()
{
    int number;
    cout<<"\n Enter any number : ";
    cin>>number;
    DecToBinary(number);
    return 0;
}

Other Categories

MCQ on C Programming MCQ on C++ Programming Basic Computer Questions Solved C programs Solved C++ programs


MCQs

About Us | Contact Us | Privacy Policy | Career  | Online Training
Youtube   LinkedIn   Facebook   Twitter   Instagram  
Copyright©CppBuzz.com
Like many websites, we use cookies to ensure best browsing experience on our website. While using this website, you acknowledge to have read and accepted our cookie and privacy policy.