Home » C Program » C Program to convert Decimal number to Octal number

C Program to convert Decimal number to Octal number

Last updated:

Support this post with a reaction:

6,599 views

Decimal to Octal conversion in C is the process of converting a number from the decimal number system (base 10) to the octal number system (base 8). The decimal number system uses digits from 0 to 9, whereas the octal number system uses only digits from 0 to 7. In C programming, this conversion can be performed by repeatedly dividing the decimal number by 8 and storing the remainders in reverse order.

What is an Octal Number?

An octal number is a number represented using the base-8 number system. It contains only eight digits, ranging from 0 to 7.

Each position in an octal number represents a power of 8, starting from \(8^0\) at the rightmost position.

For example, the octal number 154 can be converted into decimal as follows:

1548=1×82+5×81+4×80 154_8 = 1\times8^2+5\times8^1+4\times8^0

=64+40+4=108 =64+40+4=108

Therefore:

(154)8=(108)10 (154)_8=(108)_{10}

Algorithm to Convert Decimal to Octal

  1. Start.
  2. Read the decimal number.
  3. Store its absolute magnitude for conversion.
  4. If the number is zero, store 0 as the octal result.
  5. Otherwise, repeat while the number is greater than zero:
    • Calculate the remainder using number % 8.
    • Store the remainder.
    • Divide the number by 8.
  6. Print the stored remainders in reverse order.
  7. Print the negative sign if the original number was negative.
  8. Stop.

How to Convert Decimal to Octal?

We can convert a decimal number into an octal number using repeated division by 8.

Follow these steps:

  1. Divide the decimal number by 8.
  2. Store the remainder.
  3. Divide the quotient again by 8.
  4. Repeat the process until the quotient becomes 0.
  5. Read all the remainders from bottom to top.
  6. The resulting number is the octal equivalent.

Example: Convert Decimal 108 into Octal

Let’s convert decimal number 108 into octal using repeated division by 8.

DivisionQuotientRemainder
108 ÷ 8134
13 ÷ 815
1 ÷ 801

Now, read the remainders from bottom to top:

  • First remainder: 4
  • Second remainder: 5
  • Third remainder: 1

Therefore, the octal equivalent is:

(108)10=(154)8 \boxed{(108)_{10}=(154)_8}

C Program to convert decimal into octal

#include <stdio.h>

int main()
{
    long long num;
    unsigned long long n;
    char octal[65];
    int i = 0;

    printf("Enter a decimal number: ");
    if (scanf("%lld", &num) != 1)
    {
        printf("Invalid input!");
        return 1;
    }

    if (num < 0)
        n = 0ULL - (unsigned long long)num;
    else
        n = (unsigned long long)num;

    if (n == 0)
    {
        octal[i++] = '0';
    }
    else
    {
        while (n > 0)
        {
            octal[i++] = (n % 8) + '0';
            n = n / 8;
        }
    }

    printf("Octal equivalent of %lld is: ", num);

    if (num < 0)
        printf("-");

    while (i > 0)
    {
        printf("%c", octal[--i]);
    }

    printf("\n");

    return 0;
}

Output

Program to Convert Decimal Number into Octal
Please Give a Number to Convert in Octal:  34
Octal Number of 34 is =   4  2

Explanation of Decimal to Octal Program in C

  1. Include the Header File
    • #include <stdio.h> includes the standard input/output library.
    • It allows us to use printf() and scanf() functions.
  2. Declare the Variables
    • int arr[10], num, i, j;
    • arr[10] stores the remainders obtained during division by 8.
    • num stores the decimal number entered by the user.
    • i is used to control the first loop and store array positions.
    • j is used to print the array elements in reverse order.
  3. Display the Message
    • printf() displays a message asking the user to enter a decimal number.
  4. Read the Input
    • scanf("%d", &num); reads the decimal number entered by the user.
    • The entered number is stored in the variable num.
  5. Convert Decimal into Octal
    • for(i = 0; num > 0; i++) executes the loop until num becomes 0.
    • arr[i] = num % 8; calculates the remainder when the number is divided by 8 and stores it in the array.
    • num = num / 8; divides the number by 8 and updates the quotient.
    • The loop repeats until the quotient becomes 0.
  6. Store the Remainders
    • Each remainder represents an octal digit.
    • The remainders are stored in the array in reverse order.
    • Therefore, they must be printed from the last position to the first position.
  7. Print the Octal Number
    • for(j = i - 1; j >= 0; j--) starts from the last occupied array position.
    • printf("%d", arr[j]); prints each array element in reverse order.
    • This produces the correct octal number.
  8. Terminate the Program
    • return 0; indicates that the program has executed successfully.

Example: Convert Decimal 34 into Octal

IterationDecimal NumberRemainder (num % 8)Quotient (num / 8)
13424
2440
  • First remainder = 2
  • Second remainder = 4
  • Read the remainders in reverse order.
  • Final octal number = 42

Output:

Enter a decimal number: 34
Octal number = 42