In this post, you will learn **how to Convert Octal Number to Decimal** and vice-versa using C++.

This lesson will teach you how to Convert Octal Number to Decimal and vice-versa, mathematical operations, loop statements and logical operators statement using the C++ Language. Letâ€™s look at the below source code.

## How to Convert Octal Number to Decimal and vice-versa?

RUN CODE SNIPPET## Source Code

#include <iostream> #include <cmath> using namespace std; void DecimalToOctal(int decimalNum) { int octalNum = 0, placeValue = 1; int dNo = decimalNum; while (decimalNum != 0) { octalNum += (decimalNum % 8) * placeValue; decimalNum /= 8; placeValue *= 10; } cout<<"Octal form of decimal number "<<dNo<<" is "<<octalNum<<endl; } void OctalToDecimal(int octalNum) { int decimalNum = 0, power = 0; int oNo = octalNum; while(octalNum != 0) { decimalNum += (octalNum%10) * pow(8,power); ++power; octalNum/=10; } cout<<"\nDecimal form of octal number "<<oNo<<" is "<<decimalNum<<endl; } int main() { DecimalToOctal(20); OctalToDecimal(32); return 0; }

## Output

Octal form of decimal number 20 is 24 Decimal form of octal number 32 is 26

The statementsÂ **#include<iostream>, using namespace std**Â are the main factors that **support** the function of the source code. The **void keyword **is used when the function does not have a return value.Â The **#include <cmath> **is used when the additional mathematical functions are used in the code.

Now we can look into the working and layout of the codeâ€™s function.

- In this code we have two sections one to convert a
**Decimal to Octal**and one to convert a**Octal to Decimal.** - The first section is we execute the
**Decimal to Octal**Â function. - Declare the integers
*octalNum , placeValue, int dNoÂ*asÂ**integersÂ**and assign*decimalNumÂ*to the integerÂ**int dNo.** - Open aÂ
**while loopÂ**and create the loop statement with the following functions.

{ octalNum += (decimalNum % 8) * placeValue; decimalNum /= 8; placeValue *= 10; }In this we find the reminder of two integer values (**decimalNum, 8**) multiply it with an integer (**placeValue**) and using the**Add AND assignment operator**, then assign the answer to**octalNum.**Using the**Divide AND assignment operator,**perform the operation and assign the answer to**decimalNum.**

Using theÂ**Multiply AND assignment operator,**perform the operation and assign the answer toÂ**placeValue.** - Display the output statement using theÂ
**coutÂ**function with the integerÂ**dNoÂ**where the answer is stored. - The second section we executed theÂ
**Octal to Decimal.** - Declare theÂ
**voidÂ**function and and declare the integers and assign the values as follows :Â**decimalNum = 0, power = 0, oNo = octalNum.** - Declare a
**while loop**with the condition`(octalNum != 0)`

. The**loop statementÂ**has the following functions:

{ decimalNum += (octalNum%10) * pow(8,power); ++power; octalNum/=10; }In the loop statement we find theÂ**reminderÂ**of two values (octalNum,10)Â**multiplyÂ**it with the power of (8,power). TheÂ**powerÂ**function as the name finds the power of two values in the brackets, where the 1st number is the base and the second number is the power.

Next using the**Add AND assignment operator,**perform the operation with the answer from the previous stepâ€¦ thenÂ we increment the**power value.**Using the**Divide AND assignment operator,**perform the operation and assign the answer toÂ**octalNum.** - Display the output statement using theÂ
**coutÂ**function with the integer**oNoÂ**where the answer is stored. - Declare theÂ
**int mainÂ**function, to enter the values to be used in the functions. In this code we include the numbers in the code rather than getting it from the user. **DecimaltoOctal**with the number**20**and**OctaltoDecimal**with the number**32,**and return the valueÂ**o.**

**Note:** The **â€˜ << endl â€˜ **in the code is used to **end the current line **and move to the next line and **â€˜\nâ€™ **is also a **new line function,** to understand how both the functions work exclude it from the code, move it around and work with it.