C++ Program for Octal to Decimal Conversion

Octal to Decimal Conversion in C++

Here, on this page, we will discuss octal to decimal conversion in C++. Let’s have a look at an example before moving ahead with code of Octal to Decimal in C++.

(462)8 = 4 * 82 + 6 * 81 + 2 * 80
256 + 48 + 2 = (306)10
Octal to Decimal in C++

Different Methods discussed

We will discuss the following methods in the post –

  1. Algorithmic way (Octal to Decimal)
  2. Inbuilt Method (Octal to Decimal)

Method 1

Octal to Decimal Conversion in C++

Algorithm:-

  1. Initialize i = 0, decimal = 0, base = 8
  2. Extract the last digit (digit = num% 10)
  3. Calculate decimal equivalent of this digit
  4. Add it to decimal variable
    • decimal += digit * pow(8 ,i);
  5. Reduce the number (num /= 10
  6. increment i value

C++ Code:-

Run
#include<iostream>

#include<math.h>
using namespace std;

// function to convert octal to decimal
int getOctal(long long num)
{
    int i = 0, decimal = 0;
    
    // will only work for bases 2 - 10
    int base = 8;
    
    //converting octal to decimal
    while (num!=0)
    {
        int digit = num % 10;
        decimal += digit * pow(base, i);

        num /= 10;
        i++;
    }
    return decimal;
}

// main program
int main()
{
    // long used rather than int to store large values
    // Ex : int wont store 111111111111 (12 digits) as
    // limit for int is 2147483647 (10 digits)
    long long octal = 462;
    
    cout << getOctal(octal);   
    
    return 0;
}

Output

306

Method 2

C++ Code:-

Run

 

#include<iostream>
using namespace std;
 
int main()
{
    // important input has to be in string format only here
    string binaryNumber;
    cin >> binaryNumber;
    
    int base = 8;
    // format stoi(binary_in_string_format, 0, base_value)
    cout << stoi(binaryNumber, 0, base);
 
    return 0;
}

Output

462
306