C program to convert Decimal to Octal number

In this article, we are going to learn the C program to convert Decimal to Octal numbers. Here we are going to learn about two numbers one is octal and another is Decimal. Octal numbers are base 8 numbers and are represented in numbers from 0 to 7. Decimal numbers are base 10 numbers and are represented in numbers from 0 to 9.

Program to convert decimal to octal number


In this c program, we are asking users to input decimal number and by using some logic in the below program convert them to octal numbers.

#include <stdio.h>

int main()
{
    long long decimalNum, tmpDecimalNo;
    long long octalNum;
    int i, rem, place = 1;

    octalNum = 0;

    printf("Please enter the decimal number: ");
    scanf("%lld", &decimalNum);

    tmpDecimalNo = decimalNum;

    while(tmpDecimalNo > 0)
    {
        rem = tmpDecimalNo % 8;

        octalNum = (rem * place) + octalNum;

        tmpDecimalNo = tmpDecimalNo/8;

        place *= 10;
    }

    printf("\nDecimal number = %lld\n", decimalNum);
    printf("Octal number = %lld", octalNum);

    return 0;
}

Output

Please enter the decimal number: 11

Decimal number = 11
Octal number = 13