C Programming Code Examples C > For Loops and While Loops Code Examples program to convert Binary to Octal number system program to convert Binary to Octal number system Write a C program to input binary number from user and convert to octal number system. How to convert from binary number system to octal number system in C. Binary Number System Binary number system is a base 2 number system. Binary number system uses only 2 symbols to represent all its numbers i.e. 0 and 1. Octal Number System Octal number system is a base 8 number system. Octal number system uses 8 symbols to represent all its numbers i.e. 0 1 2 3 4 5 6 7 To make things simple and sound, I have divided the logic in two easy steps. Group all binary bits to 3 digits starting from right side. Write corresponding octal value for each grouped binary value. Binary to octal conversion Binary to octal conversion table Binary Octal 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7 Input binary number from user. Store it in a variable say binary. Initialize a variable to store converted octal say octal = 0. Find the last three digits of binary say digit = num % 1000. Find the octal equivalent (using binary to octal table) of the three binary digits found above. Add octal value of binary found in above step to octal, by increasing the place value. Remove the last three digits of the binary number. Since they are processed, say binary = binary / 1000. Increase the place value of octal by using place = place * 10. Repeat step 3 to 7 till binary > 0. #include <stdio.h> int main() { int octalConstant[] = {0, 1, 10, 11, 100, 101, 110, 111}; long long binary, octal, tempBinary; int digit, place, i; octal = 0; place= 1; /* Input binary number from user */ printf("Enter any binary number: "); scanf("%lld", &binary); /* Copy original binary value to temp variable */ tempBinary = binary; while(tempBinary != 0) { /* Extract last three digit of binary */ digit = tempBinary % 1000; /* Find octal equivalent of 3 digit binary */ for(i=0; i<8; i++) { if(octalConstant[i] == digit) { /* * Increase the place value of octal * and add the previous octal value */ octal = (i * place) + octal; break; } } /* Remove the last three digit of binary */ tempBinary /= 1000; /* Increase the place value */ place *= 10; } printf("Original binary number = %lld\n", binary); printf("Octal number = %lld", octal); return 0; }