C++ Program For Octal To Decimal ConversionIn 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: Output: Explanation: 1. Include Header Files: - #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.
2. namespace: - As this line declares, the program will use the std namespace. The standard C++ library is contained in the std namespace.
3. Function for Octal to Decimal Conversion: - 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.
4. Main Function: - 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.
5. Return Statement: The main function returns 0, indicating successful execution to the operating system.
|