Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Convert Octal Number to Decimal and Vice Versa in C++



In a computer system, the octal number is expressed in the octal numeral system while the decimal number is in the decimal numeral system. The octal number is in base 8 while the decimal number is in base 10. In this article, we will write a C++ program to convert an octal number to a decimal number and vice-versa.

Here are some examples showing decimal numbers and their corresponding octal values:

Decimal Number Octal Number
8 10
70 106
25 31
7 7

The image below shows how a decimal number is turned into octal number and octal number to decimal number:

Decimal to Octal and vice versa

Converting Octal to Decimal

To convert an octal number to decimal, we multiply each digit by 8 raised to the power of its position, starting from 0 at the rightmost digit. Then, we sum all these results to get the decimal number.

For example, Let's convert octal 106 to decimal:

Input: Octal Number = 106
(1 x 8^2) + (0 x 8^2) + (6 x 8^2)  
= 64 + 0 + 6  
= 70

Output:
The decimal equivalent of octal 106 is 70.

Example: C++ Program to Convert Octal to Decimal

Below is a complete C++ program where we convert an octal number into its decimal form by multiplying each digit with powers of 8, starting from the right.

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

// Change each octal digit to decimal by multiplying with the correct power of 8
int octalToDecimal(int octal) {
    int decimal = 0, power = 0;

    while (octal != 0) {
        int digit = octal % 10;      // Take the last digit of the octal number
        decimal += digit * pow(8, power);  // Change it to decimal and add to total
        octal /= 10;                 // Remove the last digit
        power++;                     // Move to the next power of 8
    }
    return decimal;
}

int main() {
    int octal = 106;              
    int result = octalToDecimal(octal);
    cout << "Octal number: " << octal << endl;
    cout << "Decimal equivalent: " << result << endl;
    return 0;
}

The output of the above program shows the conversion of a octal number to its decimal form.

Octal number: 106
Decimal equivalent: 70

Time Complexity: O(log n), where n is the number of octal digits.

Space Complexity: O(1) because we use only a few fixed variables.

Converting Decimal to Octal

To convert a decimal number to octal, we keep dividing it by 8 and note down the remainders at each step. After we're done, we simply read the remainders in reverse order. That gives us the final octal number.

For example, let's convert decimal number 70 into octal:

Input: Decimal number : 70
70 / 8 = 8 remainder 6 
8 / 8 = 1 remainder 0  
1 / 8 = 0 remainder 1  
Now, reverse the remainders: 106

Output:
The octal equivalent of decimal 70 is 106.

Example: C++ Program to Convert Decimal to Octal

Below you will see the complete C++ program for converting a decimal number to octal by repeatedly dividing by 8 and storing the remainders, which are then reversed to form the octal result.

#include <iostream>
using namespace std;

// Fucntion to convert decimal to octal
int decimalToOctal(int decimal) {
    int octal = 0, place = 1;

    while (decimal != 0) {
        int remainder = decimal % 8; // Take the remainder after dividing by 8
        octal += remainder * place; // Add it to the octal number at the right place
        decimal /= 8;   // Divide the number by 8 to move to the next digit
        place *= 10; // Increase the place value (like units, tens)
    }
    return octal;
}a

int main() {
    int decimal = 70;            
    int result = decimalToOctal(decimal);     
    cout << "Decimal number: " << decimal << endl;  // Show input number
    cout << "Octal equivalent: " << result << endl; // Show converted result
    return 0;
}

The output below shows the conversion of the decimal number 70 to its octal equivalent.

Decimal number: 70
Octal equivalent: 106

Time Complexity: O(log n), because the decimal number is divided by 8 in each step.

Space Complexity: O(1) because no extra space is used.

Updated on: 2025-05-20T20:09:25+05:30

571 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements