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).
Similar Reads
n-bit Johnson Counter in Digital Logic
Prerequisite - Counters Johnson counter also known as creeping counter, is an example of synchronous counter. In Johnson counter, the complemented output of last flip flop is connected to input of first flip flop and to implement n-bit Johnson counter we require n flip-flop. It is one of the most im
5 min read
Ripple Counter in Digital Logic
Counters play a crucial role in digital logic circuits, enabling tasks such as clock frequency division and sequencing. This article explores the concept of ripple counters, a type of asynchronous counter, their operation, advantages, and disadvantages in digital logic design. What is a Counter?Coun
5 min read
Design counter for given sequence
A Counter is a device which stores (and sometimes displays) the number of times a particular event or process has occurred, often in relationship to a clock signal. Counters are used in digital electronics for counting purpose, they can count specific event happening in the circuit. For example, in
3 min read
Master-Slave JK Flip Flop
Prerequisite -Flip-flop types and their ConversionRace Around Condition In JK Flip-flop - For J-K flip-flop, if J=K=1, and if clk=1 for a long period of time, then Q output will toggle as long as CLK is high, which makes the output of the flip-flop unstable or uncertain. This problem is called race
4 min read
Asynchronous Sequential Circuits
An asynchronous sequential circuit does not have the clock signal; the transitions between different states occur due to the âchange of inputsâ. This makes them suitable for applications which involve low power input or when a clock signal may not be needed. In this article, we explain how these cir
6 min read
Shift Registers in Digital Logic
Pre-Requisite: Flip-FlopsFlip flops can be used to store a single bit of binary data (1 or 0). However, in order to store multiple bits of data, we need multiple flip-flops. N flip flops are to be connected in order to store n bits of data. A Register is a device that is used to store such informati
8 min read
Design 101 sequence detector (Mealy machine)
A sequence detector is a sequential state machine that takes an input string of bits and generates an output 1 whenever the target sequence has been detected. In a Mealy machine, output depends on the present state and the external input (x). Hence, in the diagram, the output is written outside the
5 min read
Amortized analysis for increment in counter
Amortized analysis refers to determining the time-averaged running time for a sequence (not an individual) operation. It is different from average case analysis because here, we don't assume that the data arranged in average (not very bad) fashion like we do for average case analysis for quick sort.
4 min read
Number System and Base Conversions
Electronic and digital systems use various number systems such as Decimal, Binary, Hexadecimal and Octal, which are essential in computing. Binary (base-2) is the foundation of digital systems.Hexadecimal (base-16) and Octal (base-8) are commonly used to simplify the representation of binary data. T
9 min read
Code Converters - BCD(8421) to/from Excess-3
Prerequisite - Number System and base conversions Excess-3 binary code is an unweighted self-complementary BCD code. Self-Complementary property means that the 1's complement of an excess-3 number is the excess-3 code of the 9's complement of the corresponding decimal number. This property is useful
5 min read