C++ Program For Octal To Decimal Conversion - C++ Programming Tutorial
C++ Course / C++ Programs / C++ Program For Octal To Decimal Conversion

C++ Program For Octal To Decimal Conversion

BLUF: Mastering C++ Program For Octal To Decimal Conversion is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Program For Octal To Decimal Conversion

C++ is renowned for its efficiency. Learn how C++ Program For Octal To Decimal Conversion enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore a C++ code example that performs the conversion from octal to decimal along with detailed insights into the process.

Program:

Here is a basic C++ code snippet that converts an octal number into 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:
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:
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:
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:
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:
Example

return 0;

The primary function concludes by returning 0 to signal successful completion to the operating system.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience