Convert Octal to Decimal

Convert Octal to Decimal in C++ (6 Programs)

IntermediateTopic: Advanced Number Programs
Back

C++ Convert Octal to Decimal Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    long long octal;
    int decimal = 0, i = 0, remainder;
    
    cout << "Enter an octal number: ";
    cin >> octal;
    
    long long temp = octal;
    
    while (temp != 0) {
        remainder = temp % 10;
        temp /= 10;
        decimal += remainder * pow(8, i);
        ++i;
    }
    
    cout << "Octal: " << octal << " = Decimal: " << decimal << endl;
    
    return 0;
}
Output
Enter an octal number: 12
Octal: 12 = Decimal: 10

Understanding Convert Octal to Decimal

This program converts octal to decimal by multiplying each digit by 8 raised to its position. It demonstrates 6 different methods: using pow(), using string, using recursion, using stoi() with base 8, using manual calculation, and using functions.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Table of Contents