In the previous article, we have discussed about C++ Program to Convert Octal Number to Decimal Number. Let us learn how to Convert Decimal Number to Octal Number in C++ Program.

## Methods to convert decimal number to octal number in c++

In this article, we discuss different methods by which we can convert decimal numbers to octal numbers in c++. The methods that we will discuss today are given below.

First, discuss the basic intuition behind converting decimal to octal numbers in c++. Suppose a number is 16 and we have to find an octal form of 16 then we can do like this:-

16%8==0 octal=0 16/8=2

2%8==2 octal=20 2/8=0

and we stop as our number becomes 0. So we get octal of a number like this. Now we will discuss different methods of doing this task.

### Method 1-Using arithmetic operator with array

As we see in the example above we do the task in the same manner. We store the remainder of the number when divided by 8 in the array and after that, we divide the number by 8. We perform the following steps till our number is greater than 0. After that, we will print the elements of the array in the reverse form which will be the answer. Let’s write the code for this.

#include <iostream> using namespace std; void decimalToOctal(int n) { int octalNum[100],num=n; int i = 0; while (n > 0) { octalNum[i] = n % 8; n = n / 8; i++; } cout<<num<<" in octal form is "; for (int j = i - 1; j >= 0; j--) { cout << octalNum[j]; } } int main() { int n = 16; decimalToOctal(n); return 0; }

**Output**

16 in octal form is 20

### Method 2-Using arithmetic operator without the array

We can also do the same task without using the array. Here the idea is the same but instead of an array, we use a variable. Let’s write the code for this.

#include <bits/stdc++.h> using namespace std; int decimalToOctal(int n) { long long octalNumber = 0; int rem, i = 1, step = 1; while (n!=0) { rem = n%8; n /= 8; octalNumber += rem*i; i *= 10; } return octalNumber; } int main() { int n = 16; cout<<n<<" in octal form is "<<decimalToOctal(n); return 0; }

**Output**

16 in octal form is 20

So these are the methods to convert decimal number to octal number in c++.