You are reading the article Decimal To Octal In C updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Decimal To Octal In C
Introduction to Decimal to Octal in CThe 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.
Start Your Free Software Development Course
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:
int n=32; intoctalArray[50]; int count = 0; while (n != 0) { octalArray[count] = n % 8; n = n / 8; count++; } cout<<octalArray[m]; Examples of Decimal to Octal in CHere are the following examples mentioned:
Example #1 – Decimal to octal int main() { int number; scanf("%d", &number); printf("ntEqual octal number to decimal number is = %o", number); return 0; }Output:
Example #2 – Decimal to Octal with for loopC Code: DecBin.c
int main() { int octalArray[50], number, var1,j; scanf("%d", &number); { octalArray[var1] = number % 8; number = number / 8; } { printf("%d", octalArray[j]); } return 0; }Output:
Example #3 – Decimal to octal with the while loop int main() { int octalArray[50], number, var1,j; scanf("%d", &number); while(number!=0) { octalArray[var1] = number % 8;//remainder number = number / 8;//interger part var1++;//incrementing var1 } { printf("%d", octalArray[j]);//display octal array in reverse order } return 0; }Output:
Example #4 – Decimal to octal with functionC Code: DecBin.c
int getDecimalToOctal(int number); int main() { int number; scanf("%d", &number); int oct = getDecimalToOctal(number); return 0; } int getDecimalToOctal(int number) { int octal=0,rem, var1=1; 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:
ConclusionDecimal 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 ArticlesThis 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 –
You're reading Decimal To Octal In C
Update the detailed information about Decimal To Octal In C on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!