C Program to Convert Octal to Decimal

In this C Programming example, we will implement the program to convert a number from octal to decimal and print the output on the console.

1. How do you Convert a number from Octal to Decimal?

The octal numeral system is the base-8 number system and accepts the digits from 0 to 7. Octal numerals can be formed using binary numerals and grouping the consecutive binary digits into groups of three (starting from the right).

  1. The octal to decimal conversion process is quite similar to that of binary to decimal conversion
  2. Here, instead of base 2, we are going to use base 8.
  3. So, to begin the conversion we have to start multiplying the digits of the number from the right-hand side with increasing powers of 8.
  4. Starting from 0 and then adding up all the products.
Example
Input:
123

CONVERSION: (123)8 = (83)10
(123 % 10) * 80 = 3 * 1 = 3
(12 % 10) * 81 = 2 * 8 = 16
(1 % 10) * 82 = 1 * 64 = 64

Output:
64 + 16 + 3 = 83

Helpful topics to understand this program better are-


2. C Program to Convert Octal to Decimal

Let’s examine the execution(kind of pseudocode) of the program to convert a number from octal to decimal in C.

  1. Initially, the program will prompt the user to enter a decimal number and store the input value in int decimalNum.
  2. Now, we invoke the function convertDecimalToOctal(int decimalNum) and pass the user input decimal number to it.
  3.  Later the calculations are performed on the input using a while loop.
  4. Print the final output to the console.

Let us now implement the above execution of the program to convert a number from octal to decimal in C.

#include <stdio.h>
#include <math.h>

long convertOctalToDecimal(int octalNum);

long convertOctalToDecimal(int octalNum){
  int decimalNum = 0, i = 0;
  while(octalNum != 0){
    decimalNum += (octalNum%10) * pow(8,i);
    ++i;
    octalNum/=10;
    }
  i = 1;
  return decimalNum;
}

int main(){
  int octalNum;
  printf("Enter an octal number: ");
  scanf("%d", &octalNum);
  printf("%d in octal is %ld in decimal", octalNum, convertOctalToDecimal(octalNum));
  return 0;
}

Note: In the above program we have used int data type to store the input. However, we can use float, double, long datatype with their format specifier as well.

Output
Enter an octal number: 45
45 in octal is 37 in decimal

3. Conclusion

In this C Programming example, we have discussed how to convert a number from octal to decimal in C.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x