 # C++ Programming Code Examples

## C++ > Conversions Code Examples

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

``` C++ Program to Convert Octal Number to Decimal and vice-versa In this example, you will learn to convert octal number to decimal and decimal number to octal manually by creating a user-defined function. In the program, the octal number is stored in the variable octalNumber and passed to function octalToDecimal(). This function converts the octal number passed by user to its equivalent decimal number and returns it to main() function. #include <iostream> #include <cmath> using namespace std; int octalToDecimal(int octalNumber); int main() { int octalNumber; cout << "Enter an octal number: "; cin >> octalNumber; cout << octalNumber << " in octal = " << octalToDecimal(octalNumber) << " in decimal"; return 0; } // Function to convert octal number to decimal int octalToDecimal(int octalNumber) { int decimalNumber = 0, i = 0, rem; while (octalNumber != 0) { rem = octalNumber % 10; octalNumber /= 10; decimalNumber += rem * pow(8, i); ++i; } return decimalNumber; } ``` 