WAP to convert binary to Octal number system

Write a C program to read 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 number to octal number system in C program.
Example
Input
Input binary number: 00110111
Output
Octal number: 67

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 table
Binary
Octal
000
0
001
1
010
2
011
3
100
4
101
5
110
6
111
7
Below is the step by step logic to convert binary to octal number system.
1.            Read binary number from user. Store it in some variable say binary.
2.            Initialize a variable to store converted octal say octal = 0.
3.            Extract 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 the 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-7 till binary > 0.

Program to convert Binary to Octal

/**
 * 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


Comments

Most Viewed

Write C program to enter any year and check whether year is leap year or not using conditional/ternary operator.

WAP to input week number and print week day name.

C Program to Find Third Angle of a Triangle