
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to convert Octal Number to Decimal and vice-versa
In a computer system, the octal number is expressed in the octal numeral system while the decimal number is in the decimal numeral system. The octal number is in base 8 while the decimal number is in base 10.
Examples of decimal numbers and their corresponding octal numbers are as follows.
Decimal Number | Octal Number |
---|---|
10 | 12 |
70 | 106 |
25 | 31 |
16 | 20 |
A program that converts the octal numbers into decimal and the decimal numbers into octal is as follows −
Example
#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<<"Decimal 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
In the above program, the function DecimalToOctal convert the decimal numbers into octal and the function OctalToDecimal converts octal numbers into decimal.
In the function DecimalToOctal, initially the variable octalNum is initialized to zero. The variable placeValue denotes the location of the digit in the number. The value of octalNum is found using a while loop.
For each iteration of the while loop, the decimalNum is divided by 8 and the remainder is multiplied by placeValue. This is added to the previous value of octalNum. Also decimalNum is divided by 8 and the quotient is stored back. placeValue is multiplied by 10.
A code snippet that demonstrates this is as follows.
while (decimalNum != 0) { octalNum += (decimalNum % 8) * placeValue; decimalNum /= 8; placeValue *= 10; }
After the Octal value i.e octalNum is ovbtained, it is displayed. This is given below −
cout<<"Octal form of decimal number "<<dNo<<" is "<<octalNum<<endl;
In the function OctalToDecimal, in each iteration of the while loop octalNum is divided by 8 and the remainder is multiplied by 8 raised to power. This is added to the previous value of decimalNum. Power is incremented by 1 and octalNum is divided by 10. This is demonstrated as follows.
while(octalNum != 0) { decimalNum += (octalNum%10) * pow(8,power); ++power; octalNum/=10; }
After the value of decimalNum is found, it is displayed. This is seen as follows.
cout<<"Decimal form of octal number "<<oNo<<" is "<<decimalNum<<endl;
The main() function only contains the function calls to DecimalToOctal() and OctalToDecimal() with the required values. This is demonstrated by the following code snippet.
DecimalToOctal(20); OctalToDecimal(32);
- Related Articles
- C++ Program to Convert Binary Number to Octal and vice-versa
- C++ Program to Convert Binary Number to Decimal and vice-versa
- How to Convert Decimal Number to Binary/Octal/Hex Number or Vice Versa in Excel?
- Java Program to convert octal number to decimal number
- Convert from any base to decimal and vice versa in C++
- C# program to convert decimal to Octal number
- Java program to convert decimal number to octal value
- Java program to convert float decimal to Octal number
- Java Program to convert decimal integer to octal number
- Python program to convert float decimal to octal number
- Binary to decimal and vice-versa in Python
- Java Program to convert Decimal to Octal
- Golang Program to convert Decimal to Octal
- Swift Program to convert Decimal to Octal
- Golang program to convert the arraylist into string and vice-versa
