Binary To Octal Program In C


Implementation

Implementation of this algorithm is given below −

#include <stdio.h>

int main()
{   
    long int binary, octal=0;
    int j, remainder;

    j = 1;
    octal = 0;
    binary = 10101; 

	printf("Binary = %ld\n", binary);
	printf("Binary = %lo\n", binary);
    while(binary!=0)
    {
        remainder = binary % 10;
        octal = octal + remainder * j;
        j = j * 2;
        binary = binary / 10;
    }

    printf("Octal = %lo\n", octal);
    printf("Octal = %ld\n", octal);

    return 0;
}

Output

Output of the program should be −

Binary = 10101
Octal = 25
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements