Converting a decimal number to its octal counterpart is a common task in programming when dealing with different number systems. So, let’s say you want to convert the decimal number 156 to octal. This program shows you how to do that using simple iteration and base conversion.
Prerequisites for the Decimal to Octal Conversion Program in C
Before starting with the code, make sure you meet the following requirements:
- Knowledge of
whileloops for control flow - Understanding of the
%and/operators for arithmetic expressions - Include the
<stdio.h>library - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Convert Decimal to Octal
Here's the complete C code to convert a decimal number to octal. It uses a while loop to repeatedly divide the number by 8 and collect the remainders, which form the octal number.
// Program to convert decimal to octal
#include <stdio.h>
int main() {
int decimal, quotient, octal_number = 0, position = 1;
// Input from user
// Prompt the user to enter a decimal number
printf("Enter a decimal number: ");
scanf("%d", &decimal);
// Copy the input number to quotient
quotient = decimal;
// Convert decimal to octal
while (quotient != 0) {
int remainder = quotient % 8;
octal_number += remainder * position;
quotient /= 8;
position *= 10;
}
// Output the octal number
printf("Octal number: %d\n", octal_number);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Decimal to Octal Conversion Program
When you run the program with the input '156', here's what you get:
Enter a decimal number: 156
Octal number: 234
Algorithm to Convert Decimal to Octal in C
Converting a decimal number to octal involves dividing the number by 8 and storing the remainders. The remainders are collected in reverse order to form the octal number. Here is the exact logic this program uses, step by step.
- Read the decimal number from the user.
- Initialize
octal_numberto 0 andpositionto 1. - Repeat while the quotient is not 0:
- Find remainder by
quotient % 8. - Update
octal_numberby addingremainder * position. - Divide
quotientby 8. - Multiply
positionby 10. - Print the
octal_number.
flowchart TD
A([Start]) --> B[/Enter decimal number/]
B --> C[quotient = decimal]
C --> D[octal_number = 0]
D --> E[position = 1]
E --> F{"quotient != 0?"}
F -->|Yes| G["remainder = quotient %% 8"]
G --> H["octal_number = octal_number + remainder * position"]
H --> I["quotient = quotient / 8"]
I --> J["position = position * 10"]
J --> F
F -->|No| K[/Print octal_number/]
K --> L([End])How the C Program to Convert Decimal to Octal Works
Let's break down the code to see how each part contributes to the conversion from decimal to octal.
#include <stdio.h>: Includes the standard input-output library for usingprintfandscanf.int main(): The main function where the program execution begins.int decimal, quotient, octal_number = 0, position = 1;: Declares and initializes the needed variables. Here,octal_numberwill store the result, andpositionhelps place each digit correctly.scanf: Reads the decimal number from the user.quotient = decimal;: Copies the user input toquotientfor processing.- The
whileloop: Continues as long as there's a quotient to process. remainder = quotient % 8;: Finds the remainder whenquotientis divided by 8.octal_number += remainder * position;: Adds the new digit to the growing octal number.quotient /= 8;: Prepares for the next iteration by reducing the quotient.position *= 10;: Moves the position left by one digit place.printf: Outputs the final octal number.return 0;: Ends the program.
Quick Quiz — test your understanding
Tap an option to check your answerWhy do we multiply the remainder by the position in this program?
Time and Space Complexity of the Decimal to Octal Conversion Program
Analyzing how efficient our program is can help us understand its behavior with larger inputs.
Time Complexity: O(log n). Here, n is the decimal number being converted. This is because the program repeatedly divides quotient by 8, halving the number of digits to process each time. For example, converting 156 to octal takes only a few steps.
Space Complexity: O(1). The program uses a fixed number of variables: decimal, quotient, octal_number, and position. These are four integers regardless of the size of the input number.
Common mistake:Forgetting to multiply by the position leads to incorrect digit placement in the final octal number.
Key takeaway:Multiplying by position makes sure each digit in the octal number is correctly placed by its value.
FAQs About C Program to Convert Decimal to Octal
Here are some common questions students have about converting decimal to octal in C.
Related Programs
If you found this program helpful, check out these related programs:
- C Program to Convert Binary to Decimal with Code and Output
- C Program to Convert Hexadecimal to Decimal
- C Program to Convert Octal to Decimal: Full Code & Output
- C Program to Convert Binary to Octal with Sample Output
- C Program to Convert Binary to Decimal and Vice-Versa
Conclusion
Converting decimal to octal in C is easy once you get how remainders and positions work. Explore octal stuff more: try converting octal back to decimal. Practice more to really nail these concepts.
To boost your understanding, take our C language quiz or try this code in the online compiler.