Write a C Program to Convert Octal to Decimal.
Example,
Input: 245 Output: 165 Input: 472 Output: 314
If the input Octal Number is a char array, then we first convert char array to integer form.
In this article, we are taking the input Octal Number as integer.
How to Convert from Octal to Decimal
Suppose you have a Octal Number,
The corresponding decimal number is given by
Algorithm
Steps
Let octal be the input Octal Number
- Initialize p = 0, decimal = 0.
- Repeat the following steps while octal > 0.
- Set r = octal % 10.
- octal = octal / 10.
- decimal = decimal + r * 8p
- p = p + 1.
- Return decimal.
Program to Convert Octal to Decimal in C
#include<stdio.h> #include<math.h> int OctalToDecimal(int n) { int p = 0, decimal = 0, r; while(n>0){ // retrieving the right-most digit of n r = n % 10; // dividing n by 10 to remove the // right-most digits since it is already // scanned in previous step n = n / 10; decimal = decimal + r * pow( 8 , p ); ++p; } return decimal; } int main() { int n, i, k; printf("Enter Octal: "); scanf("%d", &n); printf("\nDecimal of Octal Number %d is : %d", n, OctalToDecimal(n)); return 0; }
Output

What to study next?
References
https://en.wikipedia.org/wiki/Octal