C++ Code Example: convert decimal to binary number

To convert a number from the decimal system to the binary system in C++ programming language, the following code example can be used.

#include <iostream>
using namespace std;

int main() {
    int binaryArray[10], num, i;
    
    cout << "Please enter a number: ";
    cin >> num;

    for (i = 0; num > 0; i++) {
        binaryArray[i] = num % 2;
        num = num / 2;
    }
    cout << "Binary value of number " << num << " is ";
    for (i = i - 1; i >= 0; i--) {
        cout << binaryArray[i];
    }
}
Output
Please enter a number: 23
Binary value of number 0 is 10111