Octal to Decimal conversion in C:
We can convert any octal number(base-8 (0 or 7)) into decimal number (base-10 (0 to 9)) by c program.
Decimal Number
Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 23, 445, 132, 0, 2 etc.
Octal Number
Octal number is a base 8 number because it ranges from 0 or 7. Any combination of digits (0 yo 7) is Octal number such as 34, 54, 77, 210 etc.
To understand this example, you should have the knowledge of the following C programming topics:
C Functions
C User-defined functions
/* Program to Convert Octal to Decimal */ #include <stdio.h> #include <math.h> long long convertOctalToDecimal(int octalNumber); int main() { int octalNumber; printf("Enter an octal number: "); scanf("%d", &octalNumber); printf("%d in octal = %lld in decimal", octalNumber, convertOctalToDecimal(octalNumber)); return 0; } long long convertOctalToDecimal(int octalNumber) { int decimalNumber = 0, i = 0; while(octalNumber != 0) { decimalNumber += (octalNumber%10) * pow(8,i); ++i; octalNumber/=10; } i = 1; return decimalNumber; }
Output
Enter an octal number: 14
14 in octal = 12 in decimal
Enter an octal number: 77
77 in octal = 63 in decimal
Programming Solutions @ rajeshshukla
Tutorials | Technical Questions | Interview Questions |
---|---|---|
C Programming C++ Programming Class 11 (Python) Class 12 (Python) |
C Language C++ Programming Python |
C Interview Questions C++ Interview Questions |