Learn Programming and technology with ease @ developerpublish.com

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

# 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 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.

## 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.

1. In this code we have two sections one to convert a Decimal to Octal and one to convert a Octal to Decimal.
2. The first section is we execute the Decimal to OctalÂ function.
3. Declare the integers octalNum , placeValue, int dNoÂ asÂ integersÂ and assign decimalNumÂ to the integerÂ int dNo.
4. 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.
5. Display the output statement using theÂ coutÂ function with the integerÂ dNoÂ where the answer is stored.
6. The second section we executed theÂ Octal to Decimal.
7. Declare theÂ voidÂ function and and declare the integers and assign the values as follows :Â decimalNum = 0, power = 0, oNo = octalNum.
8. 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.
9. Display the output statement using theÂ coutÂ function with the integer oNoÂ where the answer is stored.
10. 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.
11. 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.

Share:

### You May Also Like

#### C++ Program to Generate Multiplication Table

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

#### C++ Program to Display Fibonacci Sequence

In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

#### C++ Program to Find GCD of two Numbers

In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
• C++
• January 30, 2022