(C programming Example for Beginners)
C Program to Convert Octal Number to Decimal and vice-versa
In this example, you will learn to convert octal numbers to decimal and vice-versa manually by creating a user-defined function.
Example 1: Program to Convert Decimal to Octal
#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
Example 2: 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: 116 116 in octal = 78 in decimal
C Example for Beginners: C Program to Convert Octal Number to Decimal and vice-versa
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.