
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Decimal Numbers to Octal in C++
In a computer system, the octal number is expressed in the octal numeral system while the decimal number is in the decimal numeral system. The octal number is in base 8 while the decimal number is in base 10. In this article, we will write a C++ program that converts a decimal number into an octal number.
Examples of decimal numbers and their corresponding octal numbers are as follows.
Decimal Number | Octal Number |
---|---|
8 | 10 |
70 | 106 |
25 | 31 |
7 | 7 |
The image below shows how a decimal number is turned into octal:

We can convert a decimal number to octal using different approaches. Here are some simple ways to solve this:
Using Division Method
In this approach, we convert a decimal number to octal by dividing it by 8 again and again, and using the remainders to form the octal number from right to left.
Example
Here's a complete C++ program where we convert a decimal number to its octal equivalent.
#include <iostream> using namespace std; int main() { int decimal = 100; int originalDecimal = decimal; int octal = 0, remainder, i = 1; // Loop runs until the number becomes 0 while (decimal != 0) { remainder = decimal % 8; // Get remainder octal += remainder * i; // Building octal number decimal /= 8; // Reducing the number i *= 10; // Increasing place value } cout << "Decimal number: " << originalDecimal << endl; cout << "Octal equivalent: " << octal << endl; return 0; }
The output below shows the decimal number and its octal equivalent.
Decimal number: 100 Octal equivalent: 144
Time Complexity: O(log n) because we divide the number by 8 each time.
Space Complexity: O(1) because we use only a few fixed variables.
Using an Array to Store Remainders
In this approach, we use an array to store each remainder when dividing by 8. Then, we print the array in reverse order to get the final octal number.
Example
Below is a complete C++ program where we use an array to convert a decimal number to octal.
#include <iostream> using namespace std; int main() { int decimal = 100; int originalDecimal = decimal; int arr[50]; // array to store remainders int i = 0; // Keep dividing the number and storing remainders while (decimal != 0) { arr[i] = decimal % 8; decimal = decimal / 8; i++; } cout << "Decimal number: " << originalDecimal << endl; cout << "Octal equivalent: "; // Printing remainders in reverse for (int j = i - 1; j >= 0; j--) cout << arr[j]; return 0; }
The output below displays the result of the above program, showing the decimal number and its octal equivalent.
Decimal number: 100 Octal equivalent: 144
Time Complexity: O(log n) because we divide the number by 8 in each step.
Space Complexity: O(log n) because we store each remainder in an array, which grows with the number of digits in the octal result.
Using std::oct
This is a C++-style approach where we use the std::oct manipulator with cout to convert and display a number in octal format.
Example
In the example below, we declare a decimal number and use cout along with oct to directly print its octal equivalent.
#include <iostream> using namespace std; int main() { int num = 100; // Print the decimal input and its octal equivalent cout << "Decimal number: " << num << endl; cout << "Octal equivalent: " << oct << num << endl; return 0; }
Below you will see the output of the above program that displays the ocatal equivalent of given decimal number.
Decimal number: 100 Octal equivalent: 144
Time Complexity: O(1) because the program performs a constant number of operations.
Space Complexity: O(1) because it uses only a single integer variable.
Using Recursion
In this approach, we use recursion to convert a decimal number to octal. The function divides the number by 8 and calls itself until the number reaches zero. Then, it prints the remainders in reverse order to form the octal number.
Example
Below is a complete C++ program using this recursive method to convert decimal to octal.
#include <iostream> using namespace std; // This function prints octal digits using recursion void toOctal(int n) { if (n == 0) return; //base case toOctal(n / 8); cout << n % 8; } int main() { int num = 100; cout << "Decimal number: " << num << endl; cout << "Octal equivalent: "; if (num == 0) cout << "0"; else toOctal(num); cout << endl; return 0; }
The output below shows the result of converting a decimal number to octal using recursion.
Decimal number: 100 Octal equivalent: 144
Time Complexity: O(log n) because the function calls itself dividing the number by 8 each time.
Space Complexity: O(log n) because the number of recursive calls depends on how many digits are in the octal number.