C Program to Convert Octal Number to Decimal and vice-versa | C Program For beginners

In this example, you will learn to convert octal numbers to decimal and vice-versa manually by creating a user-defined function.

To understand this example, you should have the knowledge of the following C programming topics:

·         C Functions

·         C User-defined functions

Example 1: Program to Convert Decimal to Octal

#include < stdio.h >
#include < math.h >

// function prototype
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;
}

// function to convert decimalNumber to octal
int convertDecimalToOctal(int decimalNumber) {
    int octalNumber = 0, i = 1;

    while (decimalNumber != 0) {
        octalNumber += (decimalNumber % 8) * i;
        decimalNumber /= 8;
        i *= 10;
    }

    return octalNumber;
}


Labels : #c ,#code ,#examples ,

Post a Comment