Convert Binary to Octal in C
Advertisements
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
Google Advertisment