Decimal to octal conversion in C:
We can convert any decimal number (base-10 (0 to 9)) into Octal number(base-8 (0 or 7)) 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
#include <stdio.h> #include <math.h> int convertDecimalToOctal(int decimalNumber); int main() { int decimalNumber; printf("Enter a decimal number: "); scanf("%d", &decimalNumber); printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber)); return 0; } int convertDecimalToOctal(int decimalNumber) { int octalNumber = 0, i = 1; while (decimalNumber != 0) { octalNumber += (decimalNumber % 8) * i; decimalNumber /= 8; i *= 10; } return octalNumber; }
Output
Enter a decimal number: 78
78 in decimal = 116 in octal
Enter a decimal number: 10
10 in decimal = 12 in octal
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 |