C++ for Beginners: C++ Program to Convert Octal Number to Decimal and vice-versa

Hits: 15

(C++ programming Example for Beginners)

C++ Program to Convert Octal Number to Decimal and vice-versa

In this example, you will learn to convert octal number to decimal and decimal number to octal manually by creating a user-defined function.


Example 1: Convert Octal Number to Decimal


#include <iostream>
#include <cmath>
using namespace std;

int octalToDecimal(int octalNumber);

int main(){
   int octalNumber;
   cout << "Enter an octal number: ";
   cin >> octalNumber;
   cout << octalNumber << " in octal = " << octalToDecimal(octalNumber) << " in decimal";
   
   return 0;
}

// Function to convert octal number to decimal
int octalToDecimal(int octalNumber){
    int decimalNumber = 0, i = 0, rem;
    while (octalNumber != 0)
    {
        rem = octalNumber % 10;
        octalNumber /= 10;
        decimalNumber += rem * pow(8, i);
        ++i;
    }
    return decimalNumber;
}

Output

Enter an octal number: 2341
2341 in octal = 1249 in decimal

In the program, the octal number is stored in the variable octalNumber and passed to function octalToDecimal().

This function converts the octal number passed by user to its equivalent decimal number and returns it to main() function.

Example 2: Convert Decimal Number to Octal


#include <iostream>
#include <cmath>
using namespace std;

int decimalToOctal(int decimalNumber);

int main(){
   int decimalNumber;
   cout << "Enter a decimal number: ";
   cin >> decimalNumber;
   cout << decimalNumber << " in decimal = " << decimalToOctal(decimalNumber) << " in octal";
   
   return 0;
}

// Function to convert decimal number to octal
int decimalToOctal(int decimalNumber){
    int rem, i = 1, octalNumber = 0;
    while (decimalNumber != 0)
    {
        rem = decimalNumber % 8;
        decimalNumber /= 8;
        octalNumber += rem * i;
        i *= 10;
    }
    return octalNumber;
}
Enter an decimal number: 78
78 in decimal = 116 in octal

In the program, the decimal number is stored in the variable decimalNumber and passed to function decimalToOctal().

This function converts the decimal number passed by user to its equivalent octal number and returns it to main() function.

 

 

C++ for Beginners: C++ Program to Convert Octal Number to Decimal and vice-versa

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.