Program for Decimal to Octal Conversion
Last Updated :
05 Aug, 2024
Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent octal number. i.e convert the number with base value 10 to base value 8. The base value of a number system determines the number of digits used to represent a numeric value. For example, the binary number system uses two digits 0 and 1, the octal number system uses 8 digits from 0-7 and the decimal number system uses 10 digits 0-9 to represent any numeric value.
Examples:
Input : 16
Output: 20
Input : 10
Output: 12
Input : 33
Output: 41
Algorithm:
- Store the remainder when the number is divided by 8 in an array.
- Divide the number by 8 now
- Repeat the above two steps until the number is not equal to 0.
- Print the array in reverse order now.
For Example:
If the given decimal number is 16.
Step 1: Remainder when 16 is divided by 8 is 0. Therefore, arr[0] = 0.
Step 2: Divide 16 by 8. New number is 16/8 = 2.
Step 3: Remainder, when 2 is divided by 8, is 2. Therefore, arr[1] = 2.
Step 4: Divide 2 by 8. New number is 2/8 = 0.
Step 5: Since number becomes = 0.
Stop repeating steps and print the array in reverse order. Therefore, the equivalent octal number is 20.
The below diagram shows an example of converting the decimal number 33 to an equivalent octal number.

Below is the implementation of the above idea.
C++
// C++ program to convert a decimal
// number to octal number
#include <iostream>
using namespace std;
// function to convert decimal to octal
void decToOctal(int n)
{
// array to store octal number
int octalNum[100];
// counter for octal number array
int i = 0;
while (n != 0) {
// storing remainder in octal array
octalNum[i] = n % 8;
n = n / 8;
i++;
}
// printing octal number array in reverse order
for (int j = i - 1; j >= 0; j--)
cout << octalNum[j];
}
// Driver Code
int main()
{
int n = 33;
// Function Call
decToOctal(n);
return 0;
}
C
// C program to convert decimal
// number to octal number
#include <stdio.h>
// function to convert decimal to octal
void decToOctal(int n)
{
// array to store octal number
int octalNum[100];
// counter for octal number array
int i = 0;
while (n != 0) {
// storing remainder in octal array
octalNum[i] = n % 8;
n = n / 8;
i++;
}
// printing octal number array in reverse order
for (int j = i - 1; j >= 0; j--)
printf("%d", octalNum[j]);
}
// Driver Code
int main()
{
int n = 33;
// Function Call
decToOctal(n);
return 0;
}
Java
// Java program to convert a decimal
// number to octal number
import java.io.*;
class GFG {
// Function to convert decimal to octal
static void decToOctal(int n)
{
// array to store octal number
int[] octalNum = new int[100];
// counter for octal number array
int i = 0;
while (n != 0) {
// storing remainder in octal array
octalNum[i] = n % 8;
n = n / 8;
i++;
}
// Printing octal number array in reverse order
for (int j = i - 1; j >= 0; j--)
System.out.print(octalNum[j]);
}
// Driver Code
public static void main(String[] args)
{
int n = 33;
// Function Call
decToOctal(n);
}
}
// Contributed by Pramod Kumar
Python3
# Python3 program to convert
# a decimal number to
# octal number
# function to convert
# decimal to octal
def decToOctal(n):
# array to store
# octal number
octalNum = [0] * 100
# counter for octal
# number array
i = 0
while (n != 0):
# storing remainder
# in octal array
octalNum[i] = n % 8
n = int(n / 8)
i += 1
# printing octal number
# array in reverse order
for j in range(i - 1, -1, -1):
print(octalNum[j], end="")
# Driver Code
n = 33
# Function Call
decToOctal(n)
# This code is contributed
# by mits
C#
// C# program to convert a decimal
// number to octal number
using System;
class GFG {
// Function to convert decimal to octal
static void decToOctal(int n)
{
// array to store octal number
int[] octalNum = new int[100];
// counter for octal number array
int i = 0;
while (n != 0) {
// storing remainder in octal array
octalNum[i] = n % 8;
n = n / 8;
i++;
}
// Printing octal number array in
// reverse order
for (int j = i - 1; j >= 0; j--)
Console.Write(octalNum[j]);
}
// Driver Code
public static void Main()
{
int n = 33;
// Function Call
decToOctal(n);
}
}
// This code is contributed by nitin mittal.
JavaScript
<script>
// JavaScript program to convert a decimal
// number to octal number
// function to convert decimal to octal
function decToOctal(n)
{
// array to store octal number
let octalNum = new Array(100);
// counter for octal number array
let i = 0;
while (n != 0) {
// storing remainder in octal array
octalNum[i] = n % 8;
n = Math.floor(n / 8);
i++;
}
// printing octal number array in reverse order
for (let j = i - 1; j >= 0; j--)
document.write(octalNum[j]);
}
// Driver Code
let n = 33;
// Function Call
decToOctal(n);
// This code is contributed by Surbhi Tyagi
</script>
PHP
<?php
// PHP program to convert
// a decimal number to
// octal number
// function to convert
// decimal to octal
function decToOctal($n)
{
// array to store
// octal number
$octalNum;
// counter for octal
// number array
$i = 0;
while ($n != 0)
{
// storing remainder
// in octal array
$octalNum[$i] = $n % 8;
$n = (int)($n / 8);
$i++;
}
// printing octal number
// array in reverse order
for ( $j = $i - 1; $j >= 0; $j--)
echo $octalNum[$j];
}
// Driver Code
$n = 33;
// Function Call
decToOctal($n);
// This code is contributed
// by ajit
?>
Time Complexity: O(log N)
Auxiliary Space: O(L) where L is the number of digits in octal number.
Another Approach: (O(1) space Complexity)
This problem can also be solved without using an array using the following algorithm:
- Initialize octal num to 0 and countVal to 1 and the decimal number as n
- Find the remainder when decimal number divided by 8
- Update octal number by octalNum + (remainder * countval)
- Increase countval by countval*10
- Divide decimal number by 8
- Repeat from the second step until the decimal number is zero
Below is the implementation of the above idea:
C++
// C++ program to convert decimal
// number to octal number
#include <iostream>
using namespace std;
// function to calculate the octal value of the given
// decimal number
void decimaltoOctal(int deciNum)
{
// initializations
int octalNum = 0, countval = 1;
int dNo = deciNum;
while (deciNum != 0) {
// decimals remainder is calculated
int remainder = deciNum % 8;
// storing the octalvalue
octalNum += remainder * countval;
// storing exponential value
countval = countval * 10;
deciNum /= 8;
}
cout << octalNum << endl;
}
// Driver Code
int main()
{
int n = 33;
// Function Call
decimaltoOctal(n);
return 0;
}
C
// C program to convert decimal
// number to octal number
#include <stdio.h>
// function to calculate the octal value of the given
// decimal number
void decimaltoOctal(int deciNum)
{
int octalNum = 0, countval = 1;
int dNo = deciNum;
while (deciNum != 0) {
// decimals remainder is calculated
int remainder = deciNum % 8;
// storing the octalvalue
octalNum += remainder * countval;
// storing exponential value
countval = countval * 10;
deciNum /= 8;
}
printf("%d", octalNum);
}
// Driver Code
int main()
{
int n = 33;
// Function Call
decimaltoOctal(n);
return 0;
}
Java
// JAVA program to convert decimal
// number to octal number
import java.io.*;
class GFG {
// function to calculate the octal value of the given
// decimal number
static void octaltodecimal(int deciNum)
{
int octalNum = 0, countval = 1;
int dNo = deciNum;
while (deciNum != 0) {
// decimals remainder is calculated
int remainder = deciNum % 8;
// storing the octalvalue
octalNum += remainder * countval;
// storing exponential value
countval = countval * 10;
deciNum /= 8;
}
System.out.println(octalNum);
}
// Driver Code
public static void main(String[] args)
{
int n = 33;
// Function Call
octaltodecimal(n);
}
}
Python
# Python3 program to convert decimal
# number to octal number
# function to calculate the octal value of the given
# decimal number
def decimaltoOctal(deciNum):
# initializations
octalNum = 0
countval = 1
dNo = deciNum
while (deciNum != 0):
# decimals remainder is calculated
remainder = deciNum % 8
# storing the octalvalue
octalNum += remainder * countval
# storing exponential value
countval = countval * 10
deciNum //= 8
print(octalNum)
# Driver Code
if __name__ == '__main__':
n = 33
# Function Call
decimaltoOctal(n)
# This code is contributed by pratham76
C#
// C# program to convert decimal
// number to octal number
using System;
class GFG {
// function to calculate
// the octal value of the given
// decimal number
static void octaltodecimal(int deciNum)
{
int octalNum = 0, countval = 1;
while (deciNum != 0) {
// decimals remainder is
// calculated
int remainder = deciNum % 8;
// storing the octalvalue
octalNum += remainder * countval;
// storing exponential value
countval = countval * 10;
deciNum /= 8;
}
Console.Write(octalNum);
}
// Driver Code
public static void Main(string[] args)
{
int n = 33;
// Function Call
octaltodecimal(n);
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// Javascript program to convert decimal
// number to octal number
// function to calculate the octal value of the given
// decimal number
function decimaltoOctal(deciNum)
{
// initializations
let octalNum = 0, countval = 1;
let dNo = deciNum;
while (deciNum != 0) {
// decimals remainder is calculated
let remainder = Math.floor(deciNum % 8);
// storing the octalvalue
octalNum += remainder * countval;
// storing exponential value
countval = countval * 10;
deciNum = Math.floor(deciNum/8);
}
document.write(octalNum + "<br>");
}
// Driver Code
let n = 33;
// Function Call
decimaltoOctal(n);
//This code is contributed by Mayank Tyagi
</script>
Time Complexity: O(log N)
Auxiliary Space: O(1)
Using a predefined function
C++
#include <bits/stdc++.h>
using namespace std;
string intToOctal(int n)
{
stringstream st;
st << oct << n;
return st.str();
}
int main()
{
int n = 43;
cout << intToOctal(n);
return 0;
}
Java
// JAVA program to convert decimal
// number to octal number
import java.io.*;
class GFG {
public static void decToOct(int n)
{
System.out.println(Integer.toOctalString(n));
}
public static void main(String[] args)
{
int n = 33;
decToOct(n);
}
}
Python
# Python program to convert decimal
# number to octal number
def decToOct(n):
print(oct(n));
if __name__ == '__main__':
n = 33;
decToOct(n);
# This code is contributed by Amit Katiyar
C#
// C# program to convert decimal
// number to octal number
using System;
public class GFG
{
public static void decToOct(int n)
{
Console.WriteLine(Convert.ToString(n, 8));
}
public static void Main(String[] args)
{
int n = 33;
decToOct(n);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to convert decimal
// number to octal number
function decToOct(n)
{
document.write(n.toString(8));
}
var n = 33;
decToOct(n);
// This code contributed by Princi Singh
</script>
Time complexity: O(1).
Space complexity: O(1).
C++ Program to Convert Octal Number to Decimal and vice-versa
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem