C program to convert binary to octal number

 Here,we will find the solution of the conversion of the binary to the octal number system in the c programming language. Following is the solution of conversion:


Code:


#include <math.h>

#include <stdio.h>

int main() {

    long long bina,original;

    printf("Enter a binary number ? ");

    scanf("%lld", &bina);

    

    original=bina;


    int oct = 0, deci = 0, i = 0;


    // convert binary to decimal

    while (bina != 0) {

        deci += (bina % 10) * pow(2, i);

        i++;

        bina /= 10;

    }

    int j = 1;


    // convert to decimal to octal

    while (deci != 0) {

        oct += (deci % 8) * j;

        deci /= 8;

        j *= 10;

    }



 printf("%lld in binary= %d in octal", original, oct);

   return 0;

}

Output:

Enter a binary number ? 10101

10101 in binary= 25 in octal

Comments

Popular posts from this blog

how to cout in cpp

Flutter layout

input by cin in c++