Convert Decimal to Octal in C++

Decimal to Octal in C++ (4 Programs).

IntermediateAdvanced Number ProgramsExample 8 of 13
convert-decimal-to-octal.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int decimal;
6 long long octal = 0;
7 int remainder, i = 1;
8
9 cout << "Enter a decimal number: ";
10 cin >> decimal;
11
12 int temp = decimal;
13
14 while (temp != 0) {
15 remainder = temp % 8;
16 temp /= 8;
17 octal += remainder * i;
18 i *= 10;
19 }
20
21 cout << "Decimal: " << decimal << " = Octal: " << octal << endl;
22
23 return 0;
24}

Output

Enter a decimal number: 10
Decimal: 10 = Octal: 12

What's going on

The program converts a decimal number (base-10) to an octal number (base-8). For example:

Input decimal: 10
Output octal: 12

The conversion involves repeatedly dividing the decimal number by 8 and collecting the remainders. The octal number is formed by reading the remainders in reverse order (from last to first).

Example:

10 (decimal) → divide by 8 repeatedly → remainders: 2, 1 → reverse → 12 (octal)
64 (decimal) → remainders: 0, 0, 1 → reverse → 100 (octal)

Header File Used

This header provides:

cout for displaying output
cin for taking input from the user
1#include <iostream>

Declaring Variables

The program declares:

int decimal;

long long octal = 0;

int remainder, i = 1;

decimal stores the decimal number entered by the user.
octal stores the converted octal result (initialized to 0, long long for large results).
remainder stores the remainder from each division by 8.
i is a multiplier that represents the current position (1, 10, 100, 1000, ...).

Taking Input From the User

The program asks:

cin >> decimal;

The user enters a decimal number, for example: 10

We also create a temporary copy:

int temp = decimal;

This is important because we'll modify the number during conversion, and we need the original for display.

1cout << "Enter a decimal number: ";

The Conversion Algorithm

The core conversion uses a while loop:

while (temp != 0) {

remainder = temp % 8;

temp /= 8;

octal += remainder * i;

i *= 10;

}

Let's break down how this works step-by-step for decimal = 10:

Step 1:

remainder = 10 % 8 = 2 (remainder when dividing by 8)
temp = 10 / 8 = 1 (quotient after dividing by 8)
octal = 0 + 2 × 1 = 2
i = 1 × 10 = 10 (move to next position)

Step 2:

remainder = 1 % 8 = 1
temp = 1 / 8 = 0
octal = 2 + 1 × 10 = 12
i = 10 × 10 = 100

Step 3:

temp is now 0, so the loop stops
Final result: octal = 12

Understanding the Conversion Process

Why Divide by 8?

:

Octal is base-8, so we divide by 8 repeatedly.
Each division extracts one octal digit (0-7) as the remainder.
The quotient becomes the new number to process.

Why Read Remainders in Reverse?

:

The first remainder is the least significant digit (rightmost).
The last remainder is the most significant digit (leftmost).
We build the octal number by placing remainders from right to left.

Visual Example

(for decimal = 10):

Step 1: 10 ÷ 8 = 1 remainder 2 → octal digit: 2

Step 2: 1 ÷ 8 = 0 remainder 1 → octal digit: 1

Reading remainders from bottom to top: 1, 2

Result: 12 (octal)

How the Octal Number is Built

Position Multiplier (i)

:

i starts at 1 (ones place).
Each iteration, i is multiplied by 10 (moves to next position).
i values: 1, 10, 100, 1000, 10000, ...

Building Process

:

remainder * i places the digit at the correct position.
Example: remainder = 1, i = 10 → adds 10 to octal (places 1 at tens place).
Each new remainder is placed to the left of previous digits.

Why Multiply i by 10?

:

We're building a number where each position is 10 times the previous.
This creates: ones, tens, hundreds, thousands, ...
Matches how we write numbers (right to left: ones, tens, hundreds, ...).

Other Methods (Mentioned but not shown in code)

Method 2: Using Recursion

long long decimalToOctal(int decimal) {

if (decimal == 0) return 0;

}

Recursive approach - function calls itself.
Base case: when decimal becomes 0, return 0.
Recursive case: get remainder, recurse on quotient, then combine.
1return (decimal % 8) + 10 * decimalToOctal(decimal / 8);

Method 3: Using stringstream with oct Manipulator

stringstream ss;

ss << oct << decimal;

string octalStr = ss.str();

long long octal = stoll(octalStr);

oct manipulator tells stream to interpret number as octal.
Actually converts decimal to octal string representation.
More advanced use of stream manipulators.
1#include <sstream>
2#include <iomanip>

Method 4: Using Functions

long long decimalToOctal(int decimal) {

}

Encapsulates conversion in a separate function.
Makes code more organized and testable.
1// Encapsulate conversion logic in function
2// Makes code modular and reusable

Displaying the Result

The program prints:

Output:

Decimal: 10 = Octal: 12

This clearly shows both the original decimal number and its octal equivalent.

1cout << "Decimal: " << decimal << " = Octal: " << octal << endl;

Understanding the Division Process

Repeated Division by 8

:

Each division by 8 extracts one octal digit.
The remainder (0-7) is the octal digit.
The quotient is used for the next iteration.

When Does It Stop?

:

The process stops when the quotient becomes 0.
At this point, all octal digits have been extracted.

Example Trace

(for decimal = 64):

Step 1: 64 ÷ 8 = 8 remainder 0 → octal digit: 0

Step 2: 8 ÷ 8 = 1 remainder 0 → octal digit: 0

Step 3: 1 ÷ 8 = 0 remainder 1 → octal digit: 1

Step 4: 0 (stop)

Reading remainders from last to first: 1, 0, 0 → 100 (octal)

Common Decimal to Octal Conversions

Small Numbers

:

0 (decimal) = 0 (octal)
1-7 (decimal) = 1-7 (octal) (same for single digits)
8 (decimal) = 10 (octal)
9 (decimal) = 11 (octal)
10 (decimal) = 12 (octal)
16 (decimal) = 20 (octal)
64 (decimal) = 100 (octal)
512 (decimal) = 1000 (octal)

Pattern Recognition

:

Powers of 8 have single 1 followed by zeros: 8=10, 64=100, 512=1000
Numbers one less than power of 8 are all 7s: 7=7, 63=77, 511=777

When to Use Each Method

-

While Loop

: Best for learning - clear and straightforward, shows the algorithm.

-

Recursion

: Educational - helps understand recursive thinking.

-

stringstream with oct

: Advanced - demonstrates stream manipulators.

-

Functions

: Best for code organization - modular and reusable.

Best Practice

: Use while loop for learning, or functions for production code.

Important Considerations

Integer Overflow

:

Octal representation can be long for large decimals.
long long is used to handle larger octal numbers.
For very large decimals, consider using string method.

Leading Zeros

:

This method doesn't produce leading zeros.
Example: 8 (decimal) = 10 (octal), not 010.
If you need fixed width, pad with zeros manually.

Negative Numbers

:

This implementation doesn't handle negative numbers.
For negative numbers, convert absolute value and handle sign separately.

File Permissions

:

Unix/Linux file permissions use 3-digit octal.
Example: 755 (octal) = rwxr-xr-x permissions.
Understanding octal is essential for chmod command.

return 0;

This ends the program successfully.