C++ Program For Octal To Decimal Conversion

In this article, we will discuss the C++ program for octal to decimal conversion with its explanation.

Program:

Here's a simple C++ program to convert an octal number to its decimal equivalent:

Example

#include <iostream>
#include <cmath>
using namespace std;
int octalToDecimal(int octalNumber) {
int decimalNumber = 0, i = 0, remainder;
while (octalNumber != 0) {
remainder = octalNumber % 10;
decimalNumber += remainder * pow(8, i);
++i;
octalNumber /= 10;
}
return decimalNumber;
}
int main() {
int octalNumber;
cout << "Enter an octal number: ";
cin >> octalNumber;
int decimalNumber = octalToDecimal(octalNumber);
cout << "Decimal equivalent: " << decimalNumber << endl;
return 0;
}

Output:

Explanation:

  1. Include Header Files:
  2. Example
    
    #include <iostream>
    #include <cmath>
    
  • #include <iostream>: This line includes the header file for the input/output stream, which is required to operate on input and output.
  • #include <cmath>: This line includes the cmath header file, which is required to compute powers with the pow function.
  1. namespace:
  2. Example
    
    using namespace std;
    
  • As this line declares, the program will use the std namespace. The standard C++ library is contained in the std namespace.
  1. Function for Octal to Decimal Conversion:
  2. Example
    
    int octalToDecimal(int octalNumber) {
    int decimalNumber = 0, i = 0, remainder;
    while (octalNumber != 0) {
    remainder = octalNumber % 10;
    decimalNumber += remainder * pow(8, i);
    ++i;
    octalNumber /= 10;
    }
    return decimalNumber;
    }
    
  • An octal number is passed to the octalToDecimal function, which returns its decimal equivalent.
  • It iterates through each digit of the octal number using a while loop.
  • The modulo operator (%) is used inside the loop to calculate the remaining digit.
  • The decimal equivalent is updated by multiplying the remainder by 8, raised to the power of i ( the digit's position).
  • Until the octal number drops to zero, the loop is repeated.
  1. Main Function:
  2. Example
    
    int main() {
    int octalNumber;
    cout << "Enter an octal number: ";
    cin >> octalNumber;
    int decimalNumber = octalToDecimal(octalNumber);
    cout << "Decimal equivalent: " << decimalNumber << endl;
    return 0;
    }
    
  • The program's entry point serves as the primary function.
  • It declares a variable called octalNumber to store the octal number the user enters.
  • Using cout and cin , the user is asked to enter an octal number.
  • The user-inputted octal number is passed as an argument to the octalToDecimal function, and the outcome is saved in the decimalNumber
  • After that, the user is shown the decimal equivalent via cout.
  1. Return Statement:
  2. Example
    
    return 0;
    

The main function returns 0, indicating successful execution to the operating system.

Input Required

This code uses input(). Please provide values below: