Skip to content
Related Articles

Related Articles

Improve Article
C Program for Decimal to Binary Conversion
  • Last Updated : 05 Jul, 2018

Given a decimal number as input, we need to write a program to convert the given decimal number into equivalent binary number.

Examples:

Input : 7
Output : 111

Input : 10
Output : 1010

Input: 33
Output: 100001

C/C++




// 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];
}
  
// Driver program to test above function
int main()
{
    int n = 17;
    decToBinary(n);
    return 0;
}


Output :
10001

Please refer complete article on Program for Decimal to Binary Conversion for more details!

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.

My Personal Notes arrow_drop_up
Recommended Articles
Page :