# C++ Program to Convert Octal Number to Decimal and vice-versa

In this post you will learn how to Convert Octal Number to Decimal and vice-versa

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?

## 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**o****No**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**Octal****toDecimal**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.