How To Convert Decimal to Octal In C

Here, we’ll see how to build a C program that will translate a given decimal integer into its octal counterpart. We need first understand what a decimal number is and what an octal number is.

A decimal number has a base of 10. The number of digits required to express a numeric value depends on the base value of the underlying number system.

For example, the decimal number system employs 10 digits 0 to 9 to represent any numeric value, while the octal number system requires 8 digits from 0 to 7.

Decimal number system (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Example: Following numbers are decimal numbers having base 10

32, 128, 150, 845, 525, 52652

Octal number system (0, 1, 2, 3, 4, 5, 6, 7)

Example: Following numbers are decimal numbers having base 8

10, 22, 1214, 20563, 754261

Following table represents the decimal number and its equivalent octal number.

Decimal NumberEquivalent Octal Number
1012
87127
452704
52546146502

Why Is There A Need To Convert Decimal to Octal In C

For the computer instruction processing, addressing and programming we need to convert decimal numbers into octal numbers.

  • Programming Language: Most programming languages support the integer data formats in octal and hexadecimal. For the production of integer values in the binary number system, they offer a practical notation. In memory storage, all integer values are represented by setting the values of binary digits.
  • System Code Processing: Due to the bit sizes (the number of bits that could be processed at once) of the CPUs that were in use at the time, which were 26 and 36 bits, it was regarded as the appropriate shorthand for binary. Octal was no longer used once Processors changed to 32- and 64-bit bit sizes because, although both can still be divided by 8, they can also be divided by 16 for far greater efficiency.
  • For Linux File System: There are still areas where octal is still often used such as file permissions in UNIX (the source code for Linux, mac OS, and android, among other operating systems), to avoid having to provide support for symbols for hexadecimal.
  • Digital Display: Digital displays, which lack symbol support as well, also utilize it.Since it is less error-prone than attempting to achieve the same thing in hex, aircraft still employ octal (4-digit / 12-bit) transponder codes for communication with ATC.    

Approaches to Convert Decimal to Octal

Various approaches may be used to convert a decimal number to octal. There are some approaches listed below. 

  • Approach-1: Using Modulus (%) Operator
  • Approach-2: Using Format Specifier ( %o)
  • Approach-3: Using Division (/) and Multiplication (*) Operator
  • Approach-4: Using User Defined Function

Lets dive in details for each approach.

Approach-1: Using Modulus (%) Operator

In this approach, we divide the given input decimal number by 8 iteratively until it becomes zero and keep recording of the remainder in every step. Now let us see how we can convert a decimal number into its equivalent octal number. Following algorithm describes the steps involved for the conversion of decimal into its equivalent octal number.

Algorithm:

Step-1: Taking decimal number as an input from the user and store the value of given decimal number into a variable.

Step-2: Divide the given decimal number by 8 then record the resultant number and remainder value.

Step-3: Now, iteratively divide the resultant value of the previous step (step 2) until the new resultant number is 0. Also record the reminder values in each step.

Step-4: Now, print the reverse of all reminder values as the output equivalent octal number.

Example:  Let a Decimal Number is given as input: 99

Step 1:Divide the given decimal input number 99 by 8, the resultant number is 12 and remainder number is 3.

Step 2:Divide the previous step resultant number 12 by 8, the resultant number is 1 and remainder number is 4.

Step 3: Divide the previous step resultant number 1 by 8, the resultant number is 0 and remainder number is 1.

Step 4: As the resultant number of the previous step is 0, so stop dividing the resultant. Print the reverse of all reminders we get 143 as output.

Output:

So, the equivalent octal number is 143 (remainders in reverse order).

To demonstrates this approach, a program is created which code is given below:

Code:

// Demonstration of conversion of Decimal to Octal using the modulus(%) Operator 
#include <stdio.h>       // include header file
int main()               // main block is begin
{
  
    int DN;        // Declaration of Decimal number
    int i = 1, j, remain;
    int Octal[100];                   // Declaration of Octal array number
    printf("Enter a decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DN);                      // It store number in to DN 
    while (DN != 0) {          // Check DN is not equal to 0 
        remain = DN % 8;      // DN Divided by 8 using modulus operator 
        Octal[i++] = remain;
        DN = DN / 8;
    }
    printf("Octal number is: ");   // It print Octal number
    for (j = i - 1; j > 0; j--)
        printf("%d",Octal[j]);
    return 0;
}

Output:

Enter a decimal number: 99
Octal number is: 143

 Code Explanation:

  • The decimal number is asked from the user and stores its value in the ‘DN’ variable.
  • Using a loop, the given decimal number is iteratively divided until its value becomes 0.
  • The remainder is stored iteratively in an array named ‘Octal’ that is an octal number.
  • After that the value of the ‘Octal’ array is printed in reverse order. The reverse order is actually the desired output that is equivalent to the octal number. 

Approach-2: Using Format Specifier (%o)

Format specifier (%o) is another approach which may be used to convert decimal numbers to octal numbers. This method is accomplished via using %o format specifier in printf function with respect to the given decimal number. An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Use format specifier i.e. %o in printf() function.

Step-3: Display octal number for the given decimal number.

Code:

// Demonstration of conversion of Decimal to Octal using %o format specifier
#include <stdio.h>       // include header file
int main()               // main block is begin
{
  
    int DN;        // Declaration of Decimal number
    printf("Enter decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DN);                      // It store number in to DeciNum 
    printf("Octal number is: %o",DN);   // It print Octal number
    return 0;
}

Output:

Enter decimal number: 192
Octal number is: 300

Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask decimal number from the user
  • Use format specifier %o in printf() function during print the value of Decimal number i.e. DN in octal number.

Approach-3: Using  Division(/) and Multiplication Operator(*)

Without use of modulus operator (%), we can also convert decimal to octal using division (/) and multiplication (*) operators. This approach is defined in the following algorithm.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Divide decimal number using division operator(/)  and multiply quotient with 8 using multiplication operator.

Step-3: Remainder is computed by the difference of Decimal number and (quotient *8). 

Step-4 Store each remainder in a numeric array.

Step-5: Repeat step 2, 3,4 until input number is 0

Step-6: Display octal array in reverse order.

Code:

// Demonstration of conversion of Decimal to Octal using the division and multiplication Operator 
#include <stdio.h>       // include header file
int main()               // main block is begin
{
  
    int DN;        // Declaration of Decimal number
    int i = 1, j, remain,NR,NumRemain;
    int Octal[100];                   // Declaration of Octal array number
    printf("Enter a decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DN);                      // It store number in to DN 
    while (DN != 0) {          // Check DN is not equal to 0 
        remain = DN / 8;      // Divides DN by 8 using simple division operator  
        NR = remain * 8;
        NumRemain = DN - NR;
		Octal[i++] = NumRemain;
        DN = DN / 8;
    }
    printf("Octal number is: ");   // It print Octal number
    for (j = i - 1; j > 0; j--)
        printf("%d",Octal[j]);
    return 0;
}

Output:

Enter a decimal number: 460
Octal number is: 714

Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask a decimal number from the user.
  • Divide decimal number with 8 and compute remainder in octal numeric array using division and multiplication operators. 
  • After that the value of the ‘Octal’ array is printed in reverse order. The reverse order is actually the desired output that is equivalent to the octal number. 

Approach-4: Using User Defined Function

A decimal number may be converted to octal number using a user defined function. In this approach, a function is created which takes a decimal number and returns an octal number. An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Create a user defined function named Decimal_to_Octal(DN) which takes a decimal number as an argument. 

Step-3: Decimal_to_Octal(DNum) function converts a decimal number to octal using format specifier.

Step-4: Decimal_to_Octal(DNum) returns octal number.

Code:

// Demonstration of conversion of Decimal to Octal using the function 
#include <stdio.h>       // include header file
int Decimal_to_Octal(int);  // Here Decimal_to_Octal() Function prototype is defined
int main()               // main block is begin
{
  
    int DN;        // Declaration of Decimal number
    int Octal;                      // Declaration of Octal number
    printf("Enter decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DN);                      // It store number in to DN 
    Decimal_to_Octal(DN);      // Decimal_to_Octal function is called    
    return 0;
}
int Decimal_to_Octal(int DNum)
{
 printf("Octal number is: %o",DNum); // It print octal number	
 return 0;
}

Output:

Enter decimal number: 512
Octal number is: 1000

Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask a decimal number from the user.
  • Function Decimal_to_Octal() is created which takes DN as an argument. 
  • Decimal_to_Octal() converts a decimal number to octal using the format specifier.
  • Decimal_to_Octal() displays octal code. 

Best Approach- Format Specifier (%o)

The best approach  to convert  a decimal number to octal number is the format specifier (%o) approach. It is the best approach because:

  • It is  a short and very convenient way to decimal to octal number.
  • It may be used in any in-built function such as printf(), scanf() and user defined functions.     
  • It offers a short length of code.

Sample problems related to convert decimal to hexadecimal

Sample Problem-1: Using Approach-1

Problem Definition: Consider a computer programmer who wants to process user inputs into octal representation. Therefore create an algorithm and C program to demonstrate conversion of decimal numbers to octal representations.

Solution: An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take an input from the user in decimal number.

Step-2: Divide decimal number using modulus operator(%) until quotient is 0.

Step-3: Store each remainder in an octal number array.

Step-4: Display array in reverse order.

Code:

// Demonstration of conversion of Decimal to Octal using approach-1 
#include <stdio.h>       // include header file
int main()               // main block is begin
{
    int DN;        // Declaration of Decimal number
    int i = 1, j, remain;
    int Octal[100];                   // Declaration of Octal array number
    printf("Enter a decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DN);                      // It store number in to DN 
    while (DN != 0) {          // Check DN is not equal to 0 
        remain = DN % 8;      // Divides DN by 8 using modulus operator 
        Octal[i++] = remain;
        DN = DN / 8;
    }
    printf("Octal number is: ");   // It print Octal number
    for (j = i - 1; j > 0; j--)
        printf("%d",Octal[j]);
    return 0;
}

Output:

Enter a decimal number: 890
Octal number is: 1572

  Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask input from the user in decimal numbers.
  • Divide decimal number with 8 and store remainder in numeric array.
  • After that the value of the ‘Octal’ array is printed in reverse order. The reverse order is actually the desired output that is equivalent to the octal number. 

Sample Problem-2:Using Approach-2

Problem Definition: Consider a  company that wants to develop a digital calculator whose motive is to implement digital number system conversion. Digital calculator is able to convert one number format to another. For example to convert a decimal to binary, octal and hexadecimal as well as vice versa.  Therefore create an algorithm and  C code to demonstrate how to convert a decimal number to octal number.

Solution: An algorithm for this problem is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Use format specifier i.e. %o in printf() function.

Step-3: Display octal number for given decimal number.

Code:

// Demonstration of conversion of Decimal to Octal using approach-2 
#include <stdio.h>       // include header file
int main()               // main block is begin
{
  
    int DN;        // Declaration of Decimal number
    printf("Enter decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DN);                      // It store number in to DN 
    printf("Octal number is: %o",DN);   // It print Octal number
    return 0;
}

Output:

Enter decimal number: 230
Octal number is: 346

 Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask decimal number from the user
  • Use format specifier %o in printf() function during print the value of Decimal number i.e. DN in octal number.

Sample Problem-3: Using Approach-3

Problem Definition: Consider a company wish to implement digital display where it takes input from the user in decimal form and converts into octal representation i.e. 0-7. Therefore, create an algorithm and  C code to demonstrate these conversions.

Solution: An algorithm for this problem is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Divide decimal number using division operator(/)  and multiply quotient with 8 using multiplication operator.

Step-3: Remainder is computed by the difference of Decimal number and (quotient *8). 

Step-4 Store each remainder in a numeric array.

Step-5: Repeat step 2, 3,4 until DN is 0

Step-6: Display array in reverse order.

Code:

// Demonstration of conversion of Decimal to Octal using approach-3
#include <stdio.h>       // include header file
int main()               // main block is begin
{
  
    int DN;        // Declaration of Decimal number
    int i = 1, j, remain,NR,NumRemain;
    int Octal[100];                   // Declaration of Octal array number
    printf("Enter a decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DN);                      // It store number in to DN 
    while (DN != 0) {          // Check DN is not equal to 0 
        remain = DN / 8;      // Divides DN by 8 using simple division operator  
        NR = remain * 8;
        NumRemain = DN - NR;
		Octal[i++] = NumRemain;
        DN = DN / 8;
    }
    printf("Octal number is: ");   // It print Octal number
    for (j = i - 1; j > 0; j--)
        printf("%d",Octal[j]);
    return 0;
}

Output:

Enter a decimal number: 16
Octal number is: 20

Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask a decimal number from the user.
  • Divide decimal number with 8 and compute remainder in numeric array using division and multiplication operators. 
  • After that the value of the ‘Octal’ array is printed in reverse order. The reverse order is actually the desired output that is equivalent to the octal number. 

Sample Problem-4: Using Approach-4

Problem Definition: Implement a converter which converts a decimal number into octal representation. Therefore, create an algorithm and C code to demonstrate conversion of decimal to octal number.  

Solution: An algorithm for this problem is represented in the following steps.

 Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Create a user defined function named Decimal_to_Octal(DN) which takes a decimal number as an argument. 

Step-3: Decimal_to_Octal(DNum) function converts a decimal number to octal number.

Step-4: Decimal_to_Octal(DNum) returns octal number as output.

Code:

// Demonstration of conversion of Decimal to Octal using approach-4 
#include <stdio.h>       // include header file
int Decimal_to_Octal(int);  // Here Decimal_to_Octal() Function prototype is defined
int main()               // main block is begin
{
  
    int DN;        // Declaration of Decimal number
    int Octal;                      // Declaration of Octal number
    printf("Enter decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DN);                      // It store number in to DN 
    Decimal_to_Octal(DN);      // Decimal_to_Octal function is called    
    return 0;
}
int Decimal_to_Octal(int DNum)
{
 printf("Octal number is: %o",DNum); // It print octal number
 return 0;	
}

Output:

Enter decimal number: 67
Octal number is: 103

Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask a number from the user in decimal form.
  • Function Decimal_to_Octal() is created which takes DN as an argument. 
  • Decimal_to_Octal() converts a decimal number to octal number.
  • Decimal_to_Octal() displays an octal number for the given input. 

Conclusion

In the computer system, various types of number representation are available for the processing, storing and accessing of the data. A decimal number may be converted into different other numbers such as binary, octal, hexadecimal and vice versa.

In this article, many approaches to convert a decimal number to octal number were presented with an algorithm, C program, output and its explanation. A format specifier approach is considered as the best approach because it offers a shortened and convenient way.

Author: Rakesh Desc: Learn online to convert decimal to octal in C with examples. Best approach to change decimal to octal is the format specifier (%o) approach in C. Check sample problems.