Convert Decimal to Octal in C++
Decimal to Octal in C++ (4 Programs).
1#include <iostream>2using namespace std;34int main() {5 int decimal;6 long long octal = 0;7 int remainder, i = 1;89 cout << "Enter a decimal number: ";10 cin >> decimal;1112 int temp = decimal;1314 while (temp != 0) {15 remainder = temp % 8;16 temp /= 8;17 octal += remainder * i;18 i *= 10;19 }2021 cout << "Decimal: " << decimal << " = Octal: " << octal << endl;2223 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:
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:
Header File Used
This header provides:
1#include <iostream>
Declaring Variables
The program declares:
int decimal;
long long octal = 0;
int remainder, i = 1;
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:
Step 2:
Step 3:
Understanding the Conversion Process
Why Divide by 8?
:
Why Read Remainders in Reverse?
:
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)
:
Building Process
:
Why Multiply i by 10?
:
Other Methods (Mentioned but not shown in code)
Method 2: Using Recursion
long long decimalToOctal(int decimal) {
if (decimal == 0) return 0;
}
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);
1#include <sstream>2#include <iomanip>
Method 4: Using Functions
long long decimalToOctal(int decimal) {
}
1// Encapsulate conversion logic in function2// 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
:
When Does It Stop?
:
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
:
Pattern Recognition
:
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
:
Leading Zeros
:
Negative Numbers
:
File Permissions
:
return 0;
This ends the program successfully.