C++ Code Example: convert decimal to octal number

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

#include <iostream>
using namespace std;

int main() {
    int num, remainder, oct = 0, i = 1;

    cout << "Please enter a number: ";
    cin >> num;

    while (num != 0) {
        remainder = num % 8;
        num = num / 8;
        oct = oct + (remainder * i);
        i = i * 10;
    }

    cout << "Octal number : " << oct << endl;

    return 0;
}
Output
Please enter a number: 44
Octal number : 54