Updated April 10, 2023
Introduction to Decimal to Octal in C
The decimal number means the numbers which we are used for daily purpose like counting numbers, counting money, counting energy, counting courses, etc. These decimal numbers can be ranged between 0 to 9. 0 to 9 there 10 digits so the base of a decimal number is 10. The octal number means is which understands by assembly language devices or embedded devices like PCs, Computing devices, and used with systems like UNIVAC 1050, PDP-8, ICL 1900, and IBM mainframe, etc. Why because data stored in these devices are represented with group of3 digits each time so this can be done by the octal system. This octal number ranged between 0 to 7. 0 to 7 there are 8 digits so the base of the octal system is 8. In this topic, we are going to learn about Decimal to Octal in C.
It Passes the understandable instructions to devices like UNIVAC 1050, PDP-8, ICL 9000, IBM mainframe, etc.
Why octal represent in 3 binary digits each?
As we know the maximum octal digit from 0 to 7 is 7. This 7 can be represented as 111, not more than so all octal digits are between these ranges only so they always represent each octal digit with 3 binary digits.
How the remainder takes in octal?
How does binary to octal conversion done in C?
Algorithm to binary to octal:
- Store the remainder of the binary number if it is divisible by 8 from an array.
- Divide the binary number by 8 now.
- Do the same above 2 steps until the binary number equal to 0.
- After that display array in reverse order to show actual octal number.
Examples:
Let’s take the decimal number is 32.
- Step 1: We got the remainder as 0 when we divided by 8. So array[0]=0.
- Step 2: Now divide the 32 by 8, then 32/8=4, we got the number 4.
- Step 3: Now we got the remainder as 4 when 4 is divided by 8. So array[1]=4.
- Step 4: Now divide the new array[1] by 8, then 4/8=0, we got the next new number is 0.
- Step 5: Since the given number 32 becomes “0” so stop repeating steps and display the array in reverse order like array[1]array[0]. So the equivalent octal number to 32 is 40.
Syntax:
// binary number
int n=32;
//octal array
intoctalArray[50];
//counter for increment the variables for index values
int count = 0;
//while loop for given binary number
while (n != 0) {
//Store the octal array elements
octalArray[count] = n % 8;
n = n / 8;
count++;
}
// display octal array numbers in reverse order
for (int m= m - 1; m >= 0; m--)
cout<<octalArray[m];
Examples of Decimal to Octal in C
Here are the following examples mentioned:
Example #1 – Decimal to octal
C Code: DecBin.c
//including C libraries
#include <stdio.h>
//main method for C application
int main()
{
//declaring int variable for decimal number
int number;
//asking user to enter binary number
printf("\n\tPlease enter any decimal number====>");
scanf("%d", &number);
//Displaying output for octal number to the decimal number
printf("\n\tEqual octal number to decimal number is = %o", number);
return 0;
}
Output:
Example #2 – Decimal to Octal with for loop
C Code: DecBin.c
//including C libraries
#include <stdio.h>
//main method for C application
int main()
{
//declaring int variable for decimal number, and octal array
int octalArray[50], number, var1,j;
//asking user to enter binary number
printf("\n\tPlease enter any decimal number====>");
scanf("%d", &number);
for(var1 = 0; number> 0; var1++)
{
octalArray[var1] = number % 8;
number = number / 8;
}
printf("\n\tEquivalent Octal Number for given decimal number is ===>");
for(j = var1 - 1; j >= 0; j--)
{
printf("%d", octalArray[j]);
}
return 0;
}
Output:
Example #3 – Decimal to octal with the while loop
C Code: DecBin.c
//including C libraries
#include <stdio.h>
//main method for C application
int main()
{
//declaring int variable for decimal number, and octal array
int octalArray[50], number, var1,j;
//asking user to enter binary number
printf("\n\tPlease enter any decimal number====>");
scanf("%d", &number);
//while loop for decimal number to octal
while(number!=0)
{
octalArray[var1] = number % 8;//remainder
number = number / 8;//interger part
var1++;//incrementing var1
}
printf("\n\tEquivalent Octal Number for given decimal number is ===>");
for(j = var1 - 1; j >= 0; j--)
{
printf("%d", octalArray[j]);//display octal array in reverse order
}
return 0;
}
Output:
Example #4 – Decimal to octal with function
C Code: DecBin.c
//including C libraries
#include <stdio.h>
int getDecimalToOctal(int number);
//main method for C application
int main()
{
int number;
//asking user to enter binary number
printf("\n\tPlease enter any decimal number====>");
scanf("%d", &number);
//calling getDecimalToOctal method for display octal number to the given decimal number
int oct = getDecimalToOctal(number);
printf("\n\tEquivalent Octal Number for given decimal number is ===>%d", oct);
return 0;
}
int getDecimalToOctal(int number)
{
//declaring int variable for decimal number, and octal array
int octal=0,rem, var1=1;
//while loop for decimal number to octal
while(number != 0)
{
rem = number % 8;//finding remainder
number = number / 8;//finding inter part
octal = octal + (rem * var1);//getting octal number
var1 = var1 * 10;
}
return octal;
}
Output:
Conclusion
Decimal to binary conversion is for work with octal function systems like computing devices, IBM frameworks, etc. An octal number has 0 to 7 numbers only and the octal base is 8 as it is having 8 numbers. We can convert decimal to binary by using C predefined %oand while loop, for loop functionality.
Recommended Articles
This is a guide to Decimal to Octal in C. Here we discuss how does binary to octal conversion is done in C along with examples for better understanding. You may also look at the following articles to learn more –