C Program to Convert Binary Number to Octal


In this example, you will learn to convert binary number to octal and octal number to binary manually by creating a user-defined function.

Related Topics

Structures  Functions 


/*C Program to Convert Binary Number to Octal */
#include <stdio.h>
#include <math.h>

int convertBinarytoOctal(long long binaryNumber);
int main()
    long long binaryNumber;

    printf("Enter a binary number: ");
    scanf("%lld", &binaryNumber);

    printf("%lld in binary = %d in octal", binaryNumber, convertBinarytoOctal(binaryNumber));

    return 0;

int convertBinarytoOctal(long long binaryNumber)
    int octalNumber = 0, decimalNumber = 0, i = 0;

    while(binaryNumber != 0)
        decimalNumber += (binaryNumber%10) * pow(2,i);

    i = 1;

    while (decimalNumber != 0)
        octalNumber += (decimalNumber % 8) * i;
        decimalNumber /= 8;
        i *= 10;

    return octalNumber;


Enter a binary number: 101001
101001 in binary = 51 in octal


C Program to Add Two Complex Numbers by Passing Structure to a Function

In the structure complex, we are using float datatype for variables, real and imag since our concern is to add any two complex numbers with a possible fractional value.

In the main() function, the real part and the imaginary part of the complex numbers are taken separtely using scanf().

The structures n1 and n2 are passed as an argument of function add(). The function computes the sum and returns the structure variable temp to the main() function.

The final result is printed using printf() throught the main() function.


#struct  #return