C 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. Logic to convert binary to octal number system in C programming.

Example

Input

Input binary number: 00110111

Output

Octal number: 67

Required knowledge

Basic C programming, If statement, For loop, While loop, Nested loop

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

Logic to convert Binary to Octal number system

To make things simple and sound, I have divided the logic in two easy steps.

  1. Group all binary bits to 3 digits starting from right side.
  2. Write corresponding octal value for each grouped binary value.

Binary to octal conversion

Binary to octal conversion table

BinaryOctal
0000
0011
0102
0113
1004
1015
1106
1117

Step by step logic to convert binary to octal number system.

  1. Input binary number from user. Store it in a variable say binary.
  2. Initialize a variable to store converted octal say octal = 0.
  3. Find the last three digits of binary say digit = num % 1000.
  4. Find the octal equivalent (using binary to octal table) of the three binary digits found above.
  5. Add octal value of binary found in above step to octal, by increasing the place value.
  6. Remove the last three digits of the binary number. Since they are processed, say binary = binary / 1000.
  7. Increase the place value of octal by using place = place * 10.
  8. Repeat step 3 to 7 till binary > 0.

Program to convert Binary to Octal number system

/**
 * C program to convert binary to octal number system
 */

#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;
}

Output

Enter any binary number: 11001111
Original binary number = 11001111
Octal number = 317

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>