Convert Octal Number to Decimal and Vice-Versa in C

Exercise:

Write a C Program to convert octal number to decimal and vice-versa.

1. Convert Decimal to Octal

Click Here to View the Solution!
#include <stdio.h>
#include <math.h>
int DecToOct (int decimalN);

int main() {
    int decimalN;
    printf("Enter a decimal number: ");
    scanf("%d", &decimalN);
    printf("%d in decimal = %d in octal", decimalN, DecToOct(decimalN));
    return 0;
}

int DecToOct(int decimalN) {
    int octalN = 0, i = 1;
    while (decimalN!= 0) {
        octalN += (decimalN % 8) * i;
        decimalN /= 8;
        i *= 10;
    }
    return octalN;
}
Click Here to View the Output!
Enter a decimal number: 8765
8765 in decimal = 21075 in octal
Click Here to View the Explanation!
  • This program is used to convert a decimal number to an octal number.
  • A user-defined function DecToOct is declared taking an integer type decimal number as its argument.
  • In main(), the program requests the user to enter a decimal number and stores it in an integer variable decimalN using the scanf() function.
  • Initially, the DecToOct function is called from main with the entered integer as its parameter.
  • The DecToOct function is then initialized using a while loop which will continue its iterations until decimalN is not equal to 0.
  • A modulus of the decimalN is taken with 8, the output of which is multiplied with the value of i. an integer i is initially initialized as 1.
  • The value of decimalN is then updated by dividing with 8 since an octal number has 8 digits. Its result after each iteration is added into the variable octalN until decimal becomes 0.
  • Also, the value of i is updated by multiplying it with 10 in each iteration.
  • The final value in octalN is then returned to main for printing.

2. Convert Octal to Decimal

Click Here to View the Solution!
#include <stdio.h>
#include <math.h>
long long OctToDec(int octalN);

int main() {
    int octalN;
    printf("Enter an octal number: ");
    scanf("%d", & octalN);
    printf("%d in octal = %lld in decimal", octalN, OctToDec(octalN));
    return 0;
}

long long OctToDec(int octalN) {
    int decimalN = 0, i = 0;
    while(octalN!= 0) {
        decimalN += (octalN %10) * pow(8,i);
        ++i;
        octalN/=10;
    }
    i = 1;
    return decimalN;
}
Click Here to View the Output!
Enter an octal number: 54673
54673 in octal = 22971 in decimal
Click Here to View the Explanation!
  • This program is used to convert an Octal number to a Decimal number.
  • A user-defined function convert OctToDec is declared taking an integer type octal number as its argument.
  • In main(), the program requests the user to enter an octal number and stores it in an integer variable octalN using the scanf() function.
  • Initially, the OctToDec function is called from main with the entered integer as its parameter.
  • The OctToDec function is then initialized using a while loop which will continue its iterations until octalN is not equal to 0.
  • A modulus of the octalN is taken with 10, the output of which is multiplied with pow(8 , i). An integer i is initially initialized as 1.
  • The value of i is updated by incrementing it by one in each iteration.
  • The value of octalN is also updated by dividing with 10 since a decimal number represents 10 digits. Its result after each iteration is added into the variable octalN until decimal becomes 0.
  • The final value in decimalN is then returned to main for printing.

%d bloggers like this: