C program to convert octal number to decimal number system.
Required Knowledge
Octal number system is a base 8 number system using digits 0 and 7 whereas Decimal number system is base 10 number system and using digits from 0 to 9. Given an octal number as input from user convert it to decimal number.
For Example
2015 in Octal is equivalent to 1037 in Decimal number system.
Algorithm to convert Octal to Decimal number
Let's convert 2015(octal number) to decimal number- We multiply each octal digit with 8i and add them, where i is the position of the octal digit(starting from 0) from right side. Least significant digit is at position 0.
Decimal number = 2*83 + 0*82 + 1*81 + 5*80 = 1024 + 0 + 8 + 5 = 1037
C program to convert a octal number to decimal number
#include <stdio.h> #include <math.h> int main() { long octalNumber, decimalNumber=0; int position=0, digit; printf("Enter an Octal Number\n"); scanf("%ld", &octalNumber); /* Converting octal number to decimal number */ while(octalNumber!=0) { /* get the least significant digit of octal number */ digit = octalNumber%10; decimalNumber += digit*pow(8, position); position++; octalNumber /= 10; } printf("Decimal Number : %ld", decimalNumber); return 0; }Output
Enter an Octal Number 2015 Decimal Number : 1037
Enter an Octal Number 1234 Decimal Number : 668
Related Topics