0
0
CppProgramBeginner · 2 min read

C++ Program to Convert Decimal to Octal Number

To convert a decimal number to octal in C++, repeatedly divide the number by 8 and store the remainders; then print these remainders in reverse order using a loop and cout.
📋

Examples

Input8
Output10
Input65
Output101
Input0
Output0
🧠

How to Think About It

To convert decimal to octal, think of dividing the decimal number by 8 repeatedly. Each division gives a remainder between 0 and 7, which forms the octal digits from right to left. Collect these remainders and then reverse their order to get the octal number.
📐

Algorithm

1
Get the decimal number as input.
2
If the number is 0, print 0 and stop.
3
While the number is greater than 0, divide it by 8 and store the remainder.
4
Keep dividing the quotient by 8 until it becomes 0.
5
Print all the stored remainders in reverse order to get the octal number.
💻

Code

cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int decimal;
    cout << "Enter a decimal number: ";
    cin >> decimal;

    if (decimal == 0) {
        cout << 0 << endl;
        return 0;
    }

    vector<int> octalDigits;
    int num = decimal;

    while (num > 0) {
        octalDigits.push_back(num % 8);
        num /= 8;
    }

    cout << "Octal equivalent: ";
    for (int i = octalDigits.size() - 1; i >= 0; i--) {
        cout << octalDigits[i];
    }
    cout << endl;

    return 0;
}
Output
Enter a decimal number: 65 Octal equivalent: 101
🔍

Dry Run

Let's trace the decimal number 65 through the code to see how it converts to octal.

1

Input

decimal = 65

2

Check if zero

65 is not zero, continue

3

First division

65 / 8 = 8 remainder 1; store 1

4

Second division

8 / 8 = 1 remainder 0; store 0

5

Third division

1 / 8 = 0 remainder 1; store 1

6

Stop division

Quotient is 0, stop

7

Print octal

Print stored remainders in reverse: 1 0 1

QuotientRemainderStored Remainders
65 / 8 = 81[1]
8 / 8 = 10[1, 0]
1 / 8 = 01[1, 0, 1]
💡

Why This Works

Step 1: Divide by 8

Dividing the decimal number by 8 gives the remainder which is the least significant octal digit.

Step 2: Store remainders

Each remainder is stored in a list to keep track of octal digits from right to left.

Step 3: Reverse order

Printing the stored remainders in reverse order forms the correct octal number.

🔄

Alternative Approaches

Using recursion
cpp
#include <iostream>
using namespace std;

void decimalToOctal(int num) {
    if (num == 0) return;
    decimalToOctal(num / 8);
    cout << num % 8;
}

int main() {
    int decimal;
    cout << "Enter a decimal number: ";
    cin >> decimal;
    if (decimal == 0) cout << 0;
    else decimalToOctal(decimal);
    cout << endl;
    return 0;
}
Recursion prints octal digits in correct order without extra storage but may be harder to understand for beginners.
Using built-in bitset and manual conversion
cpp
#include <iostream>
#include <bitset>
using namespace std;

int main() {
    int decimal;
    cout << "Enter a decimal number: ";
    cin >> decimal;

    if (decimal == 0) {
        cout << 0 << endl;
        return 0;
    }

    string octal = "";
    int num = decimal;
    while (num > 0) {
        octal = to_string(num % 8) + octal;
        num /= 8;
    }

    cout << "Octal equivalent: " << octal << endl;
    return 0;
}
This approach builds the octal string directly and is simple but uses string concatenation which can be less efficient.

Complexity: O(log n) time, O(log n) space

Time Complexity

The number of divisions by 8 depends on the size of the decimal number, roughly proportional to log base 8 of the number.

Space Complexity

We store each remainder in a vector or string, so space grows with the number of octal digits, also O(log n).

Which Approach is Fastest?

Both iterative and recursive methods have similar time complexity; iterative is usually preferred for clarity and stack safety.

ApproachTimeSpaceBest For
Iterative with vectorO(log n)O(log n)Clear and easy to understand
RecursiveO(log n)O(log n)Elegant but uses call stack
String concatenationO(log n)O(log n)Simple but less efficient due to string operations
💡
Use a vector or string to store remainders and print them in reverse to get the octal number.
⚠️
Beginners often print remainders in the order they are found, which reverses the octal number.