In this post, we will learn how to convert octal to decimal using C Programming language.
We will take an octal number as an input and convert it into a decimal number using a user-defined function.
So, without further ado, let’s begin this tutorial.
C Program To Convert Octal To Decimal
// C Program To Convert Octal To Decimal #include <stdio.h> #include <math.h> long octaltodecimal(int num){ int temp = 0, decimal = 0; while (num != 0){ decimal = decimal + (num % 10) * pow(8, temp); temp++; num = num / 10; } return decimal; } int main(){ int num; // Asking for Input printf("Enter an octal number: "); scanf("%d", &num); printf("Equivalent decimal number after conversion is: %d", octaltodecimal(num)); return 0; }
Output
Enter an octal number: 64
Equivalent decimal number after conversion is: 52
How Does This Program Work ?
long octaltodecimal(int num){
int temp = 0, decimal = 0;
while (num != 0){
decimal = decimal + (num % 10) * pow(8, temp);
temp++;
num = num / 10;
}
return decimal;
}
In this program, we have defined a custom function named octaltodecimal which will convert the octal number into a decimal number.
printf("Equivalent decimal number after conversion is: %d", octaltodecimal(num));
We call this custom function in the main() function which will ultimately print the value obtained after conversion.
Conclusion
I hope after going through this post, you understand how to convert octal to decimal number using C Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read: