Convert Decimal to Octal in C++
Convert a decimal integer to octal in C++ by dividing by 8 and collecting remainders. Compare the loop with cout << oct and see how 10 becomes 12.
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}
Sample Output
Decimal: 10 = Octal: 12
Remainders 2 then 1.
In this one
- How repeated division by 8 produces octal digits
- How the remainders are placed using powers of 10
- How cout << oct does the same for output
- Why each octal digit maps to three bits
How it works
- 1
Read 10.
- 2
10 % 8 = 2, temp = 1; octal = 2.
- 3
1 % 8 = 1, temp = 0; octal = 2 + 10 = 12.
- 4
Print 12.
Edge cases
Input 0
Enter 0.
The loop does not run; octal is 0, correct.
Negative input
Enter -10.
temp % 8 gives -2 and then -1, so the program prints -12. That is the octal of 10 with a minus sign, not the two's complement form.
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
#include <iostream>
This header provides:
Declaring Variables
The program declares:
int decimal;
long long octal = 0;
int remainder, i = 1;
Taking Input From the User
The program asks:
cout << "Enter a decimal number: ";
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.
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;
return (decimal % 8) + 10 * decimalToOctal(decimal / 8);
}
Method 3: Using stringstream with oct Manipulator
#include <sstream>
#include <iomanip>
stringstream ss;
ss << oct << decimal;
string octalStr = ss.str();
long long octal = stoll(octalStr);
Method 4: Using Functions
long long decimalToOctal(int decimal) {
// Encapsulate conversion logic in function
// Makes code modular and reusable
}
Displaying the Result
The program prints:
cout << "Decimal: " << decimal << " = Octal: " << octal << endl;
Output:
Decimal: 10 = Octal: 12
This clearly shows both the original decimal number and its octal equivalent.
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
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.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Decimal 64 in octal?
Pick an answer to check it.
Question 2
What does cout << oct << 8 print?
Pick an answer to check it.
Questions people ask
Shortcut?
cout << oct << n;
Why multiply by 10 not 8?
The result is stored as a decimal-looking number, so each octal digit goes in the next decimal place.
Octal and binary link?
Each octal digit equals exactly three binary bits.