Convert Binary to Octal in C

Advertisements

Prev Tutorial Next Tutorial

Write a Program in C to Convert Binary to Octal

In this types of program we takes a binary number as input and converts it into Octal number.

  • Take a binary number as Input.
  • 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.
  • Add all the multiplied digits.
  • Here Combine the result of all groups to get the output.

Convert Binary to Octal in C

#include<stdio.h>
#include<conio.h>
 
void main()
{
    long int binary_number, octal_number = 0, j = 1, remainder;
 
    printf("Please Enter any Binary Number: ");
    scanf("%ld", &binary_number);
    while (binary_number != 0)
    {
        remainder = binary_number % 10;
        octal_number = octal_number + remainder * j;
        j = j * 2;
        binary_number = binary_number / 10;
    }
    printf("Equivalent Octal Value: %lo", octal_number);
 	getch();
}

Output

Please Enter any Binary Number: 11001
Equivalent Octal Value: 31 

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements