## C++ program to convert a decimal to an octal number

### CODE

```
#include <iostream>
//header file for I/O
using namespace std;
void Decimal_To_Octal(int decimal_Num)
{
int octal_Num = 0, placeValue = 1;
int dNo = decimal_Num;
while (decimal_Num != 0)
{
octal_Num += (decimal_Num % 8) * placeValue;
decimal_Num /= 8;
placeValue *= 10;
}
cout<<"Octal form of the decimal number "<<dNo<<" is: "<<octal_Num<<endl;
}
int main() {
Decimal_To_Octal(70);
return 0;
}
```

### OUTPUT

`Octal form of the decimal number 70 is: 106`

**You can try by yourself with the help of ****online C++ Compiler****.**

### EXPLANATION

In the above program, the function Decimal_To_Octal convert the decimal numbers into octal numbers. In the function Decimal_To_Octal, initially, the variable octal_Num is initialized to zero. The location of the digit of the number is denoted by the placeValue. The value of octal_Num is found using a while loop. For each iteration of the while loop, the decimal_Num is divided by 8, and the remainder is multiplied by the placeValue. This is then added to the previous value of octal_Num. Also, decimal_Num is divided by 8, and then the quotient is stored back. The placeValue is multiplied by 10.

### TIME AND SPACE COMPLEXITY

**Time Complexity: O(log N)**

**Space Complexity: O(1)**

As we, Initialize octal_num to 0 and placeValue to 1 and the decimal number as decimal_num and then find the remainder when decimal number divided by 8 and update the octal number by octal_Num + ((decimal_Num % 8)* placeValue) and increase placeValue by placeValue*10 and divide the decimal number by 8 and repeat the process from the second step until the decimal number is zero.

Must read __decimal to binary c++__ and __Application of Oops__

## Frequently Asked Questions

**How are octal and decimal numbers denoted?**

In computer systems, octal numbers are represented in the octal numeral system, while decimal numbers are in the decimal system. The decimal number is in base 10 using numbers 0 to 9, while the octal is in base 8 and uses 0 to 7.

**What do you mean by base value?**

The base value of a number system tells us the number of digits used to represent a numeric value.

**What is the algorithm used in the above program?**

We initialize octal_num to 0 and placeValue to 1 and the decimal number as decimal_num and then find the remainder when the decimal number is divided by 8 and update the octal number by octal_Num + ((decimal_Num % 8)* placeValue) and increase placeValue by placeValue*10 and divide the decimal number by 8 and repeat the process from the second step until the decimal number is zero.

## Conclusion

This article extensively discussed the C++ Program to Convert Decimal Numbers to Octal. We hope that this blog has helped you gain more knowledge regarding the conversion of decimal to octal numbers, check out our articles on __Coding Ninjas__ and solve __this__ question to practice.

Refer to our __Guided Path__ on __Coding Ninjas Studio__ to upskill yourself in __Data Structures and Algorithms__, __Competitive Programming__, __Binary to Decimal numbers__, __System Design__, and many more! If you want to test your competency in coding, you may check out the mock __test series__ and participate in the __contests__ hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the __problems__, __interview experiences,__ and __interview bundle__ for placement preparations.

Nevertheless, you may consider our paid __courses__ to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!