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:
Therefore:
Algorithm to Convert Decimal to Octal
- Start.
- Read the decimal number.
- Store its absolute magnitude for conversion.
- If the number is zero, store 0 as the octal result.
- Otherwise, repeat while the number is greater than zero:
- Calculate the remainder using
number % 8. - Store the remainder.
- Divide the number by 8.
- Calculate the remainder using
- Print the stored remainders in reverse order.
- Print the negative sign if the original number was negative.
- 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:
- Divide the decimal number by 8.
- Store the remainder.
- Divide the quotient again by 8.
- Repeat the process until the quotient becomes 0.
- Read all the remainders from bottom to top.
- 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.
| Division | Quotient | Remainder |
|---|---|---|
| 108 ÷ 8 | 13 | 4 |
| 13 ÷ 8 | 1 | 5 |
| 1 ÷ 8 | 0 | 1 |
Now, read the remainders from bottom to top:
- First remainder: 4
- Second remainder: 5
- Third remainder: 1
Therefore, the octal equivalent is:
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
- Include the Header File
#include <stdio.h>includes the standard input/output library.- It allows us to use
printf()andscanf()functions.
- Declare the Variables
int arr[10], num, i, j;arr[10]stores the remainders obtained during division by 8.numstores the decimal number entered by the user.iis used to control the first loop and store array positions.jis used to print the array elements in reverse order.
- Display the Message
printf()displays a message asking the user to enter a decimal number.
- Read the Input
scanf("%d", &num);reads the decimal number entered by the user.- The entered number is stored in the variable
num.
- Convert Decimal into Octal
for(i = 0; num > 0; i++)executes the loop untilnumbecomes 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.
- 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.
- 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.
- Terminate the Program
return 0;indicates that the program has executed successfully.
Example: Convert Decimal 34 into Octal
| Iteration | Decimal Number | Remainder (num % 8) | Quotient (num / 8) |
|---|---|---|---|
| 1 | 34 | 2 | 4 |
| 2 | 4 | 4 | 0 |
- 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