Program to Convert Binary to Octal

Program to Convert Binary to Octal

  • Write a program to Convert Binary to Octal in C
  • Write a program to Convert Binary to Octal in C++
  • Write a program to Convert Binary to Octal in Python
  • Write a program to Convert Binary to Octal in PHP
  • Write a program to Convert Binary to Octal in Java
  • Write a program to Convert Binary to Octal in Java Script
  • Write a program to Convert Binary to Octal in C#

Explanation:

Binary digits are grouped and then converted straight to their octal counterpart in order to change a binary number (base 2) to an octal number (base 8). This technique makes use of the fact that three binary digits are equivalent to one octal digit.

Steps for Binary to Octal Conversion

  1. Start with the binary number.
    • For example: 110101
  2. Group the binary digits into sets of 3, starting from the right.
    • Add leading zeros if necessary to ensure all groups have 3 digits.
    • Example: 110101 → 000110101 → 000 ∣ 110 ∣ 101
  3. Convert each group of 3 binary digits into its octal equivalent.
    • 000 → 0
    • 110 → 6
    • 101 → 5
  4. Combine the octal digits.
    • 065 (leading zeros are optional in the final result).

Example Conversion:

Convert 110101 to octal:

  1. Binary input: 110101
  2. Group into 3-bit chunks (from right): 000110101 → 000 ∣ 110 ∣ 101
  3. Convert groups to octal:
    • 000 → 0
    • 110 → 6
    • 101 → 5
  4. Result: 065 or 65 in octal.

Program to Convert Binary to Octal

#include <stdio.h>
#include <math.h>

int binaryToDecimal(long long n) {
    int decimal = 0, base = 1, remainder;
    while (n > 0) {
        remainder = n % 10;
        decimal += remainder * base;
        base *= 2;
        n /= 10;
    }
    return decimal;
}

void decimalToOctal(int decimal) {
    int octal[100], i = 0;
    while (decimal != 0) {
        octal[i++] = decimal % 8;
        decimal /= 8;
    }
    for (int j = i - 1; j >= 0; j--)
        printf("%d", octal[j]);
}

int main() {
    long long binary;
    printf("Enter a binary number: ");
    scanf("%lld", &binary);
    int decimal = binaryToDecimal(binary);
    printf("Octal: ");
    decimalToOctal(decimal);
    return 0;
}

#include <iostream>
#include <cmath>
using namespace std;

int binaryToDecimal(long long n) {
    int decimal = 0, base = 1;
    while (n > 0) {
        int remainder = n % 10;
        decimal += remainder * base;
        base *= 2;
        n /= 10;
    }
    return decimal;
}

void decimalToOctal(int decimal) {
    string octal = "";
    while (decimal > 0) {
        octal = to_string(decimal % 8) + octal;
        decimal /= 8;
    }
    cout << octal;
}

int main() {
    long long binary;
    cout << "Enter a binary number: ";
    cin >> binary;
    int decimal = binaryToDecimal(binary);
    cout << "Octal: ";
    decimalToOctal(decimal);
    return 0;
}

def binary_to_octal(binary):
    decimal = int(binary, 2)
    return oct(decimal).replace("0o", "")

binary = input("Enter a binary number: ")
print("Octal:", binary_to_octal(binary))

<?php
function binaryToOctal($binary) {
    $decimal = bindec($binary);
    return decoct($decimal);
}

$binary = readline("Enter a binary number: ");
echo "Octal: " . binaryToOctal($binary) . "\n";
?>

import java.util.Scanner;

public class BinaryToOctal {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a binary number: ");
        String binary = sc.nextLine();
        int decimal = Integer.parseInt(binary, 2);
        String octal = Integer.toOctalString(decimal);
        System.out.println("Octal: " + octal);
    }
}

function binaryToOctal(binary) {
    const decimal = parseInt(binary, 2);
    return decimal.toString(8);
}

const binary = prompt("Enter a binary number: ");
console.log("Octal:", binaryToOctal(binary));

using System;

class BinaryToOctal {
    static void Main() {
        Console.Write("Enter a binary number: ");
        string binary = Console.ReadLine();
        int decimalValue = Convert.ToInt32(binary, 2);
        string octal = Convert.ToString(decimalValue, 8);
        Console.WriteLine("Octal: " + octal);
    }
}

List of All Programs