## Decimal Numbers

These are the numbers which we generally use, like 6, 9875, 123, etc. They have a base of 10. The decimal numbers consist of numerals from 0 to 9.

## The logic for Converting Octal Numbers to Decimal

Before we move on to discuss the code implementation of converting an octal number into a decimal, let us first discuss the logic that we shall use in the code while converting octal to decimal.

- We start from the rightmost digit of the octal number and travel towards the leftmost digit of that number.
- The position of the rightmost digit is considered zero, and every time we move towards the left digit, the position is increased by 1. In code, we shall implement this using a position variable.
- For every digit (the value is in the range 0-7), we multiply that digit with 8 raised to the power of the position of that digit and add it to the result. We multiply it with the power of 8 because the base of the octal number is 8.
- Finally, after doing the above steps for every digit, we get the value of the decimal form as a result. We store the result in every step using a variable.

The below image demonstrates how the octal number is converted into decimal.

We have an octal number 467. In order to convert it into decimal, we start with the rightmost digit. Here the rightmost digit is 7 at position 0. We assume the position to be zero-based. Therefore the result at this level is 7 X 8^{0} = 7. We assume a variable, say, sum. The sum at this level is 7. For the second digit, the result is 6 X 8 = 48. The sum until the second digit is 7+48 = 55. For the third digit, the result is 4 X 64 = 256. The sum after calculating the result of the third digit is 55+256= 311. This sum is the decimal representation of the octal number 467.

Also see, __Application of Oops__

## C++ Program to Convert Octal Number to Decimal

Input: An octal number consisting of digits from 0 to 7.

Expected output: The decimal form of the octal number provided in the input.

### Code

```
// C++ program to convert an Octal number to Decimal
#include <iostream>
#include<cmath>
using namespace std;
// this function receives a number in octal form
// and returns the corresponding value in decimal form
int octalToDecimal(int octal) {
int temp = octal;
// position variable to keep track of the position of the digits
// from right to left, position increases from 0, 1, 2,...,n
int position = 0;
// this stores the sum which is calculated at each step
// while converting from octal to decimal
int sum = 0;
while (temp > 0) {
// this will give the rightmost digit
// e.g. 467%10 will give 7.
int rightMostDigit = temp % 10;
sum += rightMostDigit * pow(8, position);
// increment position so that we move on to the next digit on the left hand side
position++;
// this will remove the rightmost digit
// e.g. 467/10 = 46
temp = temp / 10;
}
return sum;
}
// main function where the code is tested
int main() {
int num;
cout << "Enter an Octal Number.\n";
cin >> num;
int decimalForm = octalToDecimal(num);
cout << "The Decimal form of the given octal number is : " << decimalForm;
return 0;
}
```

You can also try this code with Online C++ Compiler

Run Code
### Input

467

### Output

Enter an Octal Number.

467

The Decimal form of the given octal number is : 311

### Time Complexity

The time complexity is O(number of digits), because the loop iterates for the number of times equal to the number of digits.

### Space Complexity

The space complexity is O(1), because no extra space is used.

**Try and compile by yourself with the help of ****online C++ Compiler**** for better understanding.**

Read More - __Time Complexity of Sorting Algorithms__

## If The Input is In The Form of String

Since the value passed in the octalToDecimal() function is an integer, we cannot provide a very large value of the octal number to the function. Consider an example where we have an octal number whose length is 35 or even more. An integer type variable cannot store such a huge number. The best way is to take such a number in string format as input and do computation and accordingly convert it to decimal.

### Code

```
// C++ program to convert an octal string to Decimal
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
// this function receives a string in octal form
// and returns the corresponding integer value in decimal form
int octalToDecimal(string octalNum){
string num = octalNum;
// position variable to keep track of the position of the bits
// from right to left, position increases from 0, 1, 2,...,n
int position = 0;
int sum=0;
// length() determines the length of the given string
int len = num.length();
for(int i= len-1; i>=0;i--){
// the digit variable stores the numeric value by subtracting '0' as a character.
int digit= num[i]-'0';
// at each step sum is calculated
sum+= digit * pow(8, position);
// increment the position variable
position++;
}
// return the sum. It is the decimal form of the octal string provided.
return sum;
}
int main() {
string octalNum;
cout<<"Enter an Octal number.\n";
cin>>octalNum;
int decimal = octalToDecimal(octalNum);
cout<<"The decimal form is : "<<decimal;
return 0;
}
```

You can also try this code with Online C++ Compiler

Run Code
### Input** **

651472365

### Output

Enter an Octal number.

651472365

The decimal form is : 111572213

### Time Complexity

The time complexity is O(number of digits), because the loop iterates for the number of times equal to the number of digits.

### Space Complexity

The space complexity is O(1), because no extra space is used.

## Alternate Way

Instead of using pow(8, position), we can use a base variable that will be multiplied by 8 after every iteration.

### Code

```
// C++ program to convert Octal string to Decimal
// without using pow() function
#include <iostream>
#include <string>
using namespace std;
// this function receives a string in Octal form
// and returns the corresponding integer value in decimal form
int octalToDecimal(string octalNum){
string num = octalNum;
// this represents the rightmost position.
// observe that we have assigned base=1 and not 0
// because at position =0
// pow(8,0)=1
int base= 1;
int sum=0;
// length() determines the length of the given string
int len = num.length();
for(int i= len-1; i>=0;i--){
// the digit variable stores the numeric value by subtracting '0' as a character.
int digit = num[i]-'0';
/* In the first iteration the digit will be multiplied by 1
In the second iteration the digit will be multiplied by 8
.
.
.
In the nth iteration the digit will be multiplied by 8^(n-1)
*/
sum+= digit*base;
// multiply base by 8 after every iteration
base= base *8;
}
// return the sum. This is the decimal form of the given octal string
return sum;
}
int main() {
string octalNum;
cout<<"Enter an Octal number.\n";
cin>>octalNum;
int decimal = octalToDecimal(octalNum);
cout<<"The decimal form is : "<<decimal;
return 0;
}
```

You can also try this code with Online C++ Compiler

Run Code
### Input

563274156

### Output

Enter the binary number.

563274156

The decimal form is : 97351790

### Time Complexity

The time complexity is O(number of digits), because the loop iterates for the number of times equal to the number of digits.

### Space Complexity

The space complexity is O(1), because no extra space is used.

## Using Predefined Function

We can use the stoi() function in C++ to convert an Octal number given as a string into a decimal number.

### Code

```
// using stoi() in C++ to convert Octal to decimal
#include <iostream>
using namespace std;
int main() {
string octalNum;
cout<<"Enter an Octal number.\n";
cin>> octalNum;
// C++ stoi() convert Octal string to decimal
int decimal = stoi(octalNum, 0, 8);
cout<<"The decimal form is : "<<decimal;
return 0;
}
```

You can also try this code with Online C++ Compiler

Run Code
### Input** **

4675677

### Output

Enter an Octal number.

4675677

The decimal form is : 1276863

### Time Complexity

The time complexity of stoi() function is O(number of digits).

### Space Complexity

The space complexity is O(1), because no extra space is used.

Check out this article - __C++ String Concatenation__

Must read __decimal to binary c++__ and, __Dynamic Binding in C++__.

## Frequently Asked Questions

**What is an Octal number?**

The octal number system is made up of eight different numerals, from 0 to 7. The octal numbers have a base of 8.

**What is the base of Octal numbers?**

The base of an octal number is 8.

**Which predefined function in C++ can be used to convert an Octal string into a decimal number?**

We can use the stoi() function in C++ to convert an octal string into a decimal number.

**What will be the value of the octal number 763254637 in the decimal system?**

130898335

## Conclusion

In this article, we have discussed what Octal numbers are and the __C++__ program to convert octal numbers to decimal numbers. You should definitely read this blog to get a more clear understanding of __number systems__.

Refer to our __guided paths__ on __Code Studio__ to upskill yourself in __Data Structures and Algorithms__, __Competitive Programming__, __JavaScript__, __System Design__, and many more! If you want to test your competency in coding, you may check out the mock __test series__ and participate in __contests__ hosted on Code Studio! But if you have just started your learning process and looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must have a look at the __problems__, __interview experiences__, and __interview bundle__ for placement preparations.

Nevertheless, you may consider our paid __courses__ to give your career an edge over others!

Do upvote our blog if you find them helpful and engaging!

Happy Learning!