Q:

C program to decimal to octal number using the math library function

belongs to collection: Conversion Programs in C

0

We can convert decimal to octal using the pow library function declared in math.h header file. Let’s see the code.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
//typedef to avoid long name
typedef unsigned long long ULLINT;
// Function to return the octal
// equivalent of decimal value num
ULLINT decimalToOctal(ULLINT num)
{
    // To store the octal number
    ULLINT binNum = 0;
    int cnt = 0;
    while (num != 0)
    {
        int rem = num % 8;
        ULLINT c = pow(10, cnt);
        binNum += rem * c;
        num /= 8;
        // Count used to store exponent value
        cnt++;
    }
    return binNum;
}
int main()
{
    //num for decimal number
    ULLINT num;
    printf("Enter decimal number: ");
    scanf("%lld", &num);
    //Called function
    printf("%lld",decimalToOctal(num));
    return 0;
}

Output:

Enter a decimal number: 74
112

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to convert Hexadecimal to Decimal... >>
<< C program to decimal to octal number without array...