# Program for Decimal to Octal Conversion

• Difficulty Level : Easy
• Last Updated : 24 Dec, 2021

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:

1. Store the remainder when the number is divided by 8 in an array.
2. Divide the number by 8 now
3. Repeat the above two steps until the number is not equal to 0.
4. 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.
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 = 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 ``using` `namespace` `std;` `// function to convert decimal to octal``void` `decToOctal(``int` `n)``{` `    ``// array to store octal number``    ``int` `octalNum;` `    ``// 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;``}`

## 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``;` `        ``// 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.`

## PHP

 `= 0; ``\$j``--)``        ``echo` `\$octalNum``[``\$j``];``}` `// Driver Code``\$n` `= 33;` `// Function Call``decToOctal(``\$n``);` `// This code is contributed``// by ajit``?>`

## Javascript

 ``
Output
`41`

### 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 ``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 ` `// 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);``    ``}``}`

## Python3

 `# 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

 ``
Output
`41`

Time Complexity: O(log N)

Auxiliary Space: O(1)

Using a predefined function

## 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);``    ``}``}`

## Python3

 `# 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

 ``

## C++

 `#include ``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;``}`
Output
`41`

This article is contributed by Saurabh Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
{"ft_lang_label":"__label__en","ft_lang_prob":0.50468564,"math_prob":0.98909587,"size":10742,"snap":"2022-05-2022-21","text_gpt3_token_len":3292,"char_repetition_ratio":0.19724344,"word_repetition_ratio":0.42269075,"special_character_ratio":0.3129771,"punctuation_ratio":0.12939297,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9998628,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-20T21:37:23Z\",\"WARC-Record-ID\":\"<urn:uuid:18681d9e-8733-47b3-8583-9744df8ff8d9>\",\"Content-Length\":\"238006\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:42fa69ad-f5ad-4c55-8978-9804c22b1428>\",\"WARC-Concurrent-To\":\"<urn:uuid:201e7c3d-d78e-47d0-b210-d924fe4aac55>\",\"WARC-IP-Address\":\"23.218.216.136\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/program-decimal-octal-conversion/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:JD5PMG5RDIIB3LJOAY665FSVAIZLO75E\",\"WARC-Block-Digest\":\"sha1:OLWG7U2L5KOISBFO5GXTQYIAYSFXTKIL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662534669.47_warc_CC-MAIN-20220520191810-20220520221810-00320.warc.gz\"}"}