This is a C program to Convert Binary to octal.
This program takes a binary number as input and converts to octal.
1. Take a binary number as input.
2. Divide the binary number into groups of 3 bits. For each group of 3 bits, multiply each bit with the power of 2 and add them consecutively.
3. Combine the result of all groups to get the output.
Here is source code of the C program to Convert Binary to Octal. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Convert Binary to Octal
*/
#include <stdio.h>
int main()
{
long int binarynum, octalnum = 0, j = 1, remainder;
printf("Enter the value for binary number: ");
scanf("%ld", &binarynum);
while (binarynum != 0)
{
remainder = binarynum % 10;
octalnum = octalnum + remainder * j;
j = j * 2;
binarynum = binarynum / 10;
}
printf("Equivalent octal value: %lo", octalnum);
return 0;
}
1. Take a binary number as input and store it in the variable binarynum.
2. Obtain the remainder and quotient of the input number by dividing it by 10.
3. Multiply the obtained remainder with variable j and increment the variable octalnum with this value.
4. Increment the variable j by 2 and override the variable binarynum with the quotient obtained.
5. Repeat the steps 2-4 until the variable binarynum becomes zero.
6. Print the variable octalnum as output.
Output: Enter the value for binary number: 10101 Equivalent octal value: 25
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Check Computer Science Books
- Apply for C Internship
- Apply for Computer Science Internship