CppBuzz.com
  
Home C C++ Java Python Perl PHP SQL Javascript Linux Forum

You are here : Home » C++ » Program to convert a decimal number to binary number

C++ program to convert a 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;
}