C++ Program to Converter a Decimal to Binary

Last Updated:

Converter a Number from Decimal to Binary

In the following example, we will convert a Decimal Number (500) to Binary Number (111110100).

Tips: It is recommended to use our online Decimal to Binary calculator for better understanding.

Example

C++ Compiler
#include <iostream> using namespace std; int main() { int num = 500; int arr[50], j; int i = -1; while(num != 0) { i++; arr[i] = num % 2; num = num / 2; } cout << "Binary number of 500 (decimal) is "; // reverse the array and display for(j=i; j>=0; j--) cout << arr[j]; return 0; }

Converter any Given Decimal Number to Binary Number

In the following example, we will convert any given decimal number to a binary number.

Example

C++ Compiler
#include <iostream> using namespace std; int main() { int num, copyNum, arr[50], j; int i = -1; cout << "Enter a Decimal Number: "; cin >> num; copyNum = num; while(copyNum != 0) { i++; arr[i] = copyNum % 2; copyNum = copyNum / 2; } cout << "Binary number of " << num << " (decimal) is "; // reverse the array and display for(j=i; j>=0; j--) cout << arr[j]; return 0; }

Share this Page

Meet the Author